Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2018
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 5.45 KB | None | 0 0
  1. #!/bin/sh
  2. # Custom DDNS (dynamic DNS) for the no-ip.com or asuswrt service for asuswrt-merlin
  3. # The scripts works in a double NAT setup and single NAT setup, and will automatically detect the current configuration.
  4. # The script does only support single wan configuration, e.g. not dual.
  5.  
  6. #Path to logfile leave empty if not used
  7. LOGFILE=""
  8.  
  9. #Use cron to check for new ip every X minute.
  10. #Leave empty if you're using the default from merlin (24 hours).
  11. #Any previous, or if used, setting will be removed if left empty.
  12. CUSTOM_UPDATE_INTERVAL=""
  13. SILENT="false" # "true|false". true only logs ip change
  14.  
  15. #Select DDNS provider below. Select either and adjust config accordingly
  16. DDNS_PROVIDER="noip" # "noip" or "asuswrt"
  17.  
  18. ###Config for noip
  19. USERNAME=""
  20. PASSWORD=""
  21. HOSTNAME=""
  22. USERAGENT="asuswrt-merlin No-IP Updater/$VERSION"
  23.  
  24. ###Config for asuswrt
  25. MY_DDNS_HOSTNAME="[your-hostname].asuscomm.com"
  26.  
  27. #Select IP Service Provider below.
  28. IP_PROVIDER="wget -O - --no-check-certificate --quiet https://checkip.amazonaws.com/"
  29. #IP_PROVIDER="curl -s http://icanhazip.com/"
  30. #IP_PROVIDER="curl -s http://ipv4.myip.dk/api/info/IPv4Address | cut -d "\"" -f2"
  31.  
  32. ## CODE BELOW ####
  33. VERSION="3.0"
  34. # Get the the reported wan_ipaddr from nvram. (dual wan uses wan_ipaddr_x, not supported)
  35. ASUSIP="$(nvram get wan0_ipaddr)"
  36.  
  37. LogMe(){
  38.     if [[ -n "$LOGFILE" ]]; then
  39.         echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $1" >> "$LOGFILE"
  40.     fi
  41.     if [[ "$SILENT" == "true" ]]; then
  42.         if [[ -n "$2" ]]; then
  43.             logger "$1"
  44.         fi
  45.     else
  46.         logger "$1"
  47.     fi
  48. }
  49. UpdateMerlin(){
  50.     /sbin/ddns_custom_updated "$1"
  51. }
  52. UpdateIp(){
  53.     nvram set EXTERNALIP="$NEWIP"
  54.     case "$DDNS_PROVIDER" in
  55.         "noip" )
  56.             # update ip
  57.             URL="https://$USERNAME:$PASSWORD@dynupdate.no-ip.com/nic/update?hostname=$HOSTNAME&myip=$NEWIP"
  58.             RESPONSE=$(curl -s -k --user-agent "$USERAGENT" "$URL")
  59.             RESPONSE_A=$(echo $RESPONSE | awk '{ print $1 }')
  60.             case "$RESPONSE_A" in
  61.                 "good")    UpdateMerlin 1; LogMe "CustomUpdateDDNS: (good) DNS hostname successfully updated to $NEWIP." "log"
  62.                 ;;
  63.                 "nochg") UpdateMerlin 1; LogMe "CustomUpdateDDNS: (nochg) IP address is current: $NEWIP; no update performed."
  64.                 ;;
  65.                 "nohost") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (nohost) Hostname supplied does not exist under specified account. Revise config file."
  66.                 ;;
  67.                 "badauth") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (badauth) Invalid username password combination."
  68.                 ;;
  69.                 "badagent")    UpdateMerlin 0; LogMe "CustomUpdateDDNS: (badagent) Client disabled - No-IP is no longer allowing requests from this update script."
  70.                 ;;
  71.                 "!donator")    UpdateMerlin 0; LogMe "CustomUpdateDDNS: (!donator) An update request was sent including a feature that is not available."
  72.                 ;;
  73.                 "abuse") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (abuse) Username is blocked due to abuse."
  74.                 ;;
  75.                 "911") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (911) A fatal error on our side such as a database outage. Retry the update in no sooner than 30 minutes."
  76.                 ;;
  77.                 "*") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (error) Could not understand the response from No-IP. The DNS update server may be down."
  78.                 ;;
  79.             esac
  80.         ;;
  81.         "asuswrt" )
  82.             RESPONSE=$(nslookup $MY_DDNS_HOSTNAME ns1.asuscomm.com | tail -n1 | sed -e 's/Address 1: //')
  83.             if [[ -n "$RESPONSE" ]]; then
  84.                 UPDATE_DDNS="ez-ipupdate -S dyndns -i eth0 -a $NEWIP -h $MY_DDNS_HOSTNAME -A 2 -s nwsrv-ns1.asus.com"
  85.                 UpdateMerlin 1
  86.                 LogMe "CustomUpdateDDNS: DNS hostname successfully updated to $NEWIP." "log"
  87.             else
  88.                 LogMe "CustomUpdateDDNS: Something went wrong????"
  89.             fi
  90.         ;;
  91.     esac
  92. }
  93.  
  94.  
  95. CronUpdate(){
  96.     if [[ -n "$CUSTOM_UPDATE_INTERVAL" ]]; then
  97.         if [[ -z "$(cru l | grep "CustomUpdateDDNS")" ]]; then
  98.             /usr/sbin/cru a CustomUpdateDDNS "*/$CUSTOM_UPDATE_INTERVAL * * * * /jffs/scripts/ddns-start"
  99.             LogMe "CustomUpdateDDNS has been added to cron (x $CUSTOM_UPDATE_INTERVAL mins)"
  100.         fi
  101.     else
  102.         if [[ -n "$(cru l | grep "CustomUpdateDDNS")" ]]; then
  103.             /usr/sbin/cru d "CustomUpdateDDNS"
  104.             LogMe "CustomUpdateDDNS has been removed from cron"
  105.         fi
  106.     fi
  107. }
  108.  
  109. LogMe "CustomUpdateDDNS: Starting custom DDNS updater v$VERSION"
  110.  
  111. if [[ -z "$ASUSIP" ]]; then
  112.     LogMe "(error) Router has no ip or no network configuration"
  113.     exit 1
  114. fi
  115.  
  116. CronUpdate
  117.  
  118. LogMe "CustomUpdateDDNS: Reported asus router ip: $ASUSIP"
  119.  
  120. if [[ -n "$(echo "$ASUSIP" | grep -E '^(10\.|100\.(6[4-9]|7[0-9]|8[0-9]|9[0-9]|1[0-2][0-9])\.|172\.(1[6789]|2[0-9]|3[01])\.|192\.0\.0\.|192\.168|198\.1[89])')" ]]; then
  121.     # check if we have a local ip. If true, then look up external ip
  122.     LogMe "CustomUpdateDDNS: Local ip detected"
  123.     # look up external ip
  124.     NEWIP=$(eval $IP_PROVIDER)
  125.     if [[ -n "$NEWIP" ]]; then
  126.         LogMe "CustomUpdateDDNS: Found external ip: $NEWIP"
  127.     else
  128.         LogMe "CustomUpdateDDNS: External IP not found"
  129.     fi
  130. else
  131.     NEWIP="$ASUSIP"
  132.     LogMe "CustomUpdateDDNS: External ip detected"
  133. fi
  134.  
  135. # compare found ip with stored ip. If nothing is stored, assume an update is needed
  136.  
  137. if [[ "$NEWIP" == "$(nvram get EXTERNALIP)" ]]; then
  138.     # 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
  139.     LogMe "CustomUpdateDDNS: (nochange) External IP address is current: $NEWIP"
  140.     LogMe "CustomUpdateDDNS: Update not needed"
  141.     /sbin/ddns_custom_updated 1
  142. else
  143.     UpdateIp
  144. fi
  145. LogMe "CustomUpdateDDNS: DDNS update complete"
  146. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement