Advertisement
Guest User

Untitled

a guest
Aug 25th, 2016
833
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 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.4"
  5. USERNAME=""
  6. PASSWORD=""
  7. HOSTNAME=""
  8. USERAGENT="asuswrt-merlin No-IP Updater/$version"
  9. ASUSIP="$(nvram get wan0_ipaddr)" # $1 could have been used, but doesn't work in cron mode, so we get the the reported wan_ipaddr from nvram instead. (dual wan uses wan_ipaddr_x, not supported)
  10. LOGFILE="" # leave empty if not used
  11. 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.
  12. ## CODE BELOW ####
  13. LogMe(){
  14. if [[ -n "$LOGFILE" ]]; then
  15. echo "[$(date +'%Y-%m-%d %H:%M:%S')]: $1" >> "$LOGFILE"
  16. fi
  17. logger "$1"
  18. }
  19. UpdateMerlin(){
  20. /sbin/ddns_custom_updated "$1"
  21. }
  22. UpdateIp(){
  23. # update ip
  24. nvram set EXTERNALIP="$NEWIP"
  25. URL="https://$USERNAME:$PASSWORD@dynupdate.no-ip.com/nic/update?hostname=$HOSTNAME&myip=$NEWIP"
  26. RESPONSE=$(curl -s -k --user-agent "$USERAGENT" "$URL")
  27. RESPONSE_A=$(echo $RESPONSE | awk '{ print $1 }')
  28. case "$RESPONSE_A" in
  29. "good") UpdateMerlin 1; LogMe "CustomUpdateDDNS: (good) DNS hostname(s) successfully updated to $NEWIP."
  30. ;;
  31. "nochg") UpdateMerlin 1; LogMe "CustomUpdateDDNS: (nochg) IP address is current: $NEWIP; no update performed."
  32. ;;
  33. "nohost") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (nohost) Hostname supplied does not exist under specified account. Revise config file."
  34. ;;
  35. "badauth") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (badauth) Invalid username password combination."
  36. ;;
  37. "badagent") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (badagent) Client disabled - No-IP is no longer allowing requests from this update script."
  38. ;;
  39. "!donator") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (!donator) An update request was sent including a feature that is not available."
  40. ;;
  41. "abuse") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (abuse) Username is blocked due to abuse."
  42. ;;
  43. "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."
  44. ;;
  45. "*") UpdateMerlin 0; LogMe "CustomUpdateDDNS: (error) Could not understand the response from No-IP. The DNS update server may be down."
  46. ;;
  47. esac
  48. }
  49. CronUpdate(){
  50. if [[ -n "$CUSTOM_UPDATE" ]]; then
  51. if [[ -z "$(cru l | grep "CustomUpdateDDNS")" ]]; then
  52. /usr/sbin/cru a CustomUpdateDDNS "*/$CUSTOM_UPDATE * * * * /jffs/scripts/ddns-start"
  53. LogMe "CustomUpdateDDNS has been added to cron (x $CUSTOM_UPDATE mins)"
  54. fi
  55. else
  56. if [[ -n "$(cru l | grep "CustomUpdateDDNS")" ]]; then
  57. /usr/sbin/cru d "CustomUpdateDDNS"
  58. LogMe "CustomUpdateDDNS has been removed from cron"
  59. fi
  60. fi
  61. }
  62.  
  63. LogMe "CustomUpdateDDNS: Starting custom DDNS updater v$VERSION"
  64.  
  65. if [[ -z "$ASUSIP" ]]; then
  66. LogMe "(error) Router has no ip or no network configuration"
  67. exit 1
  68. fi
  69.  
  70. CronUpdate
  71.  
  72. LogMe "CustomUpdateDDNS: Reported asus router ip: $ASUSIP"
  73.  
  74. 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
  75. # check if we have a local ip. If true, then look up external ip
  76. LogMe "CustomUpdateDDNS: Local ip detected"
  77. # look up external ip
  78. NEWIP=$(curl -s http://ipv4.myip.dk/api/info/IPv4Address | cut -d "\"" -f2)
  79. # backup ipcheck if first one fails
  80. if [[ -z "$NEWIP" ]]; then
  81. NEWIP=$(curl -s http://icanhazip.com/)
  82. fi
  83. LogMe "CustomUpdateDDNS: Found external ip: $NEWIP"
  84. else
  85. NEWIP="$ASUSIP"
  86. LogMe "CustomUpdateDDNS: External ip detected"
  87. fi
  88.  
  89. # compare found ip with stored ip. If nothing is stored, assume an update is needed
  90.  
  91. if [[ "$NEWIP" == "$(nvram get EXTERNALIP)" ]]; then
  92. # 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
  93. LogMe "CustomUpdateDDNS: (nochange) External IP address is current: $NEWIP"
  94. LogMe "CustomUpdateDDNS: Update not needed"
  95. /sbin/ddns_custom_updated 1
  96. else
  97. UpdateIp
  98. fi
  99. LogMe "CustomUpdateDDNS: DDNS update complete"
  100. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement