Advertisement
Guest User

Untitled

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