Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2016
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. #!/bin/sh
  2. # Custom DDNS (dynamic DNS) for the no-ip.com service for asuswrt-merlin
  3. # The scripts works in a double NAT setup and single NAT setup, and will automatically detect the configuration
  4. # version 2.0
  5.  
  6. USERNAME=""
  7. PASSWORD=""
  8. HOSTNAME=""
  9. USERAGENT="asuswrt-merlin No-IP Updater/2.0"
  10. ASUSIP="$1" # passed to script, however, we have local ip so external providers to find ip is used
  11. LOGFILE="/mnt/sda1/jffs/ddns.log" # leave empty if not used
  12. 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.
  13.  
  14.  
  15. ### CODE BELOW ####
  16. UpdateIp(){
  17. # update ip
  18. nvram set EXTERNALIP="$NEWIP"
  19. URL="https://$USERNAME:$PASSWORD@dynupdate.no-ip.com/nic/update?hostname=$HOSTNAME&myip=$NEWIP"
  20. RESPONSE=$(curl -s -k --user-agent "$USERAGENT" "$URL")
  21. RESPONSE_A=$(echo $RESPONSE | awk '{ print $1 }')
  22. if [ "$RESPONSE_A" == "good" ]; then
  23. /sbin/ddns_custom_updated 1
  24. LOGLINE="(good) DNS hostname(s) successfully updated to $NEWIP."
  25. elif [ "$RESPONSE_A" == "nochg" ]; then
  26. /sbin/ddns_custom_updated 1
  27. LOGLINE="(nochg) IP address is current: $NEWIP; no update performed."
  28. elif [ "$RESPONSE_A" == "nohost" ]; then
  29. /sbin/ddns_custom_updated 0
  30. LOGLINE="(nohost) Hostname supplied does not exist under specified account. Revise config file."
  31. elif [ "$RESPONSE_A" == "badauth" ]; then
  32. /sbin/ddns_custom_updated 0
  33. LOGLINE="(badauth) Invalid username password combination."
  34. elif [ "$RESPONSE_A" == "badagent" ]; then
  35. /sbin/ddns_custom_updated 0
  36. LOGLINE="(badagent) Client disabled - No-IP is no longer allowing requests from this update script."
  37. elif [ "$RESPONSE_A" == "!donator" ]; then
  38. /sbin/ddns_custom_updated 0
  39. LOGLINE="(!donator) An update request was sent including a feature that is not available."
  40. elif [ "$RESPONSE_A" == "abuse" ]; then
  41. /sbin/ddns_custom_updated 0
  42. LOGLINE="(abuse) Username is blocked due to abuse."
  43. elif [ "$RESPONSE_A" == "911" ]; then
  44. /sbin/ddns_custom_updated 0
  45. LOGLINE="(911) A fatal error on our side such as a database outage. Retry the update in no sooner than 30 minutes."
  46. else
  47. /sbin/ddns_custom_updated 0
  48. LOGLINE="(error) Could not understand the response from No-IP. The DNS update server may be down."
  49. fi
  50. }
  51. Cronupdate(){
  52. if [ -n "$CUSTOM_UPDATE" ]; then
  53. if [[ -n "$(cru l | grep "CustomUpdateDDNS")" ]]; then
  54. /usr/sbin/cru a CustomUpdateDDNS "*/$CUSTOM_UPDATE * * * * /jffs/scripts/ddns-start"
  55. LOGLINE="(added to cron) CustomUpdateDDNS has been added to cron"
  56. echo "$LOGLINE"
  57. logger "CustomUpdateDDNS: $LOGLINE"
  58. if [ -n "$LOGFILE" ]; then
  59. echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $LOGLINE" >> "$LOGFILE"
  60. fi
  61. fi
  62. else
  63. if [[ -z "$(cru l | grep "CustomUpdateDDNS")" ]]; then
  64. /usr/sbin/cru d "CustomUpdateDDNS"
  65. LOGLINE="(removed from cron) CustomUpdateDDNS has been removed from cron"
  66. logger "CustomUpdateDDNS: $LOGLINE"
  67. echo "$LOGLINE"
  68. if [ -n "$LOGFILE" ]; then
  69. echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $LOGLINE" >> "$LOGFILE"
  70. fi
  71. fi
  72. fi
  73. }
  74.  
  75. logger "CustomUpdateDDNS: Executing ddns-start script"
  76.  
  77. Cronupdate
  78.  
  79. logger "CustomUpdateDDNS: Reported asus router ip: $ASUSIP"
  80. if [[ -n "$(echo "$ASUSIP" | grep -E '^(192\.168|10\.|172\.1[6789]\.|172\.2[0-9]\.|172\.3[01]\.)')" ]]; then
  81. # check if we have a local ip. If true, then look up external ip
  82. logger "CustomUpdateDDNS: Local ip detected"
  83. # look up external ip
  84. NEWIP=$(curl -s http://ipv4.myip.dk/api/info/IPv4Address | cut -d "\"" -f2)
  85. # backup ipcheck if first one fails
  86. if [ -z "$NEWIP" ]; then
  87. NEWIP=$(curl -s http://icanhazip.com/)
  88. fi
  89. logger "CustomUpdateDDNS: Found external ip: $NEWIP"
  90. else
  91. logger "CustomUpdateDDNS: External ip detected"
  92. if [ -n "$LOGFILE" ]; then
  93. echo "[$(date +'%Y-%m-%d %H:%M:%S')]: External ip detected" >> "$LOGFILE"
  94. fi
  95. fi
  96. # load old ip from nvram
  97. OLDIP=$(nvram get EXTERNALIP)
  98.  
  99. if [ "$NEWIP" == "$OLDIP" ]; then
  100. # 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
  101. LOGLINE="(nochange) External IP address is current: $NEWIP."
  102. /sbin/ddns_custom_updated 1
  103. else
  104. UpdateIp
  105. fi
  106. if [ -n "$LOGFILE" ]; then
  107. # update logs
  108. logger "CustomUpdateDDNS: $LOGLINE"
  109. echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $LOGLINE" >> "$LOGFILE"
  110. fi
  111. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement