Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # Custom DDNS (dynamic DNS) for the no-ip.com service for asuswrt-merlin
- # The scripts works in a double NAT setup and single NAT setup, and will automatically detect the configuration
- # version 2.0
- USERNAME=""
- PASSWORD=""
- HOSTNAME=""
- USERAGENT="asuswrt-merlin No-IP Updater/2.0"
- ASUSIP="$1" # passed to script, however, we have local ip so external providers to find ip is used
- LOGFILE="/mnt/sda1/jffs/ddns.log" # leave empty if not used
- CUSTOM_UPDATE="" # use cron to check for new ip every X minute. Leave empty if you're using the default from merlin (24 hours). Any previous, or if used, setting will be removed if left empty.
- ### CODE BELOW ####
- UpdateIp(){
- # update ip
- nvram set EXTERNALIP="$NEWIP"
- URL="https://$USERNAME:$PASSWORD@dynupdate.no-ip.com/nic/update?hostname=$HOSTNAME&myip=$NEWIP"
- RESPONSE=$(curl -s -k --user-agent "$USERAGENT" "$URL")
- RESPONSE_A=$(echo $RESPONSE | awk '{ print $1 }')
- if [ "$RESPONSE_A" == "good" ]; then
- /sbin/ddns_custom_updated 1
- LOGLINE="(good) DNS hostname(s) successfully updated to $NEWIP."
- elif [ "$RESPONSE_A" == "nochg" ]; then
- /sbin/ddns_custom_updated 1
- LOGLINE="(nochg) IP address is current: $NEWIP; no update performed."
- elif [ "$RESPONSE_A" == "nohost" ]; then
- /sbin/ddns_custom_updated 0
- LOGLINE="(nohost) Hostname supplied does not exist under specified account. Revise config file."
- elif [ "$RESPONSE_A" == "badauth" ]; then
- /sbin/ddns_custom_updated 0
- LOGLINE="(badauth) Invalid username password combination."
- elif [ "$RESPONSE_A" == "badagent" ]; then
- /sbin/ddns_custom_updated 0
- LOGLINE="(badagent) Client disabled - No-IP is no longer allowing requests from this update script."
- elif [ "$RESPONSE_A" == "!donator" ]; then
- /sbin/ddns_custom_updated 0
- LOGLINE="(!donator) An update request was sent including a feature that is not available."
- elif [ "$RESPONSE_A" == "abuse" ]; then
- /sbin/ddns_custom_updated 0
- LOGLINE="(abuse) Username is blocked due to abuse."
- elif [ "$RESPONSE_A" == "911" ]; then
- /sbin/ddns_custom_updated 0
- LOGLINE="(911) A fatal error on our side such as a database outage. Retry the update in no sooner than 30 minutes."
- else
- /sbin/ddns_custom_updated 0
- LOGLINE="(error) Could not understand the response from No-IP. The DNS update server may be down."
- fi
- }
- Cronupdate(){
- if [ -n "$CUSTOM_UPDATE" ]; then
- if [[ -n "$(cru l | grep "CustomUpdateDDNS")" ]]; then
- /usr/sbin/cru a CustomUpdateDDNS "*/$CUSTOM_UPDATE * * * * /jffs/scripts/ddns-start"
- LOGLINE="(added to cron) CustomUpdateDDNS has been added to cron"
- echo "$LOGLINE"
- logger "CustomUpdateDDNS: $LOGLINE"
- if [ -n "$LOGFILE" ]; then
- echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $LOGLINE" >> "$LOGFILE"
- fi
- fi
- else
- if [[ -z "$(cru l | grep "CustomUpdateDDNS")" ]]; then
- /usr/sbin/cru d "CustomUpdateDDNS"
- LOGLINE="(removed from cron) CustomUpdateDDNS has been removed from cron"
- logger "CustomUpdateDDNS: $LOGLINE"
- echo "$LOGLINE"
- if [ -n "$LOGFILE" ]; then
- echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $LOGLINE" >> "$LOGFILE"
- fi
- fi
- fi
- }
- logger "CustomUpdateDDNS: Executing ddns-start script"
- Cronupdate
- logger "CustomUpdateDDNS: Reported asus router ip: $ASUSIP"
- if [[ -n "$(echo "$ASUSIP" | grep -E '^(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.)')" ]]; then
- # check if we have a local ip. If true, then look up external ip
- logger "CustomUpdateDDNS: Local ip detected"
- # look up external ip
- NEWIP=$(curl -s http://ipv4.myip.dk/api/info/IPv4Address | cut -d "\"" -f2)
- # backup ipcheck if first one fails
- if [ -z "$NEWIP" ]; then
- NEWIP=$(curl -s http://icanhazip.com/)
- fi
- logger "CustomUpdateDDNS: Found external ip: $NEWIP"
- else
- logger "CustomUpdateDDNS: External ip detected"
- if [ -n "$LOGFILE" ]; then
- echo "[$(date +'%Y-%m-%d %H:%M:%S')]: External ip detected" >> "$LOGFILE"
- fi
- fi
- # load old ip from nvram
- OLDIP=$(nvram get EXTERNALIP)
- if [ "$NEWIP" == "$OLDIP" ]; then
- # ip has not changed there's no need to hammer the ddns provider, so compare it to the previosuly found ip and save in ram
- LOGLINE="(nochange) External IP address is current: $NEWIP."
- /sbin/ddns_custom_updated 1
- else
- UpdateIp
- fi
- if [ -n "$LOGFILE" ]; then
- # update logs
- logger "CustomUpdateDDNS: $LOGLINE"
- echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $LOGLINE" >> "$LOGFILE"
- fi
- exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement