Advertisement
briped

GratisDNS.sh

Mar 3rd, 2012
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.52 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # User Configuration
  4. __USERNAME__=MyUserName
  5. __PASSWORD__=MyPassword
  6. __HOSTNAME__=host.example.com
  7. __DOMAIN__=example.com
  8.  
  9. # System Configuration
  10. # Set the __INTERFACE__ varible to nothing (__INTERFACE__=) to use external lookup.
  11. __INTERFACE__=eth1
  12. __IP_CACHE__=/tmp/gratisdns_ddns_ip.txt
  13. __LOG__=/var/log/gratisdns_ddns.log
  14.  
  15. # Get the last known DDNS IP
  16. if [ ! -e ${__IP_CACHE__} ]; then
  17.     touch ${__IP_CACHE__}
  18. fi
  19. __PREVIOUS_IP__=$(cat ${__IP_CACHE__})
  20.  
  21. # Get the current IP
  22. if [ -z "${__INTERFACE__}" ]; then
  23.     __IP__=$(curl -s "http://automation.whatismyip.com/n09230945.asp")
  24. else
  25.     __IP__=$(ifconfig ${__INTERFACE__} | sed '/inet\ /!d;s/.*r://g;s/\ .*//g')
  26. fi
  27.  
  28. if [ "${__PREVIOUS_IP__}" == "${__IP__}" ]; then
  29.     # Identical IP's, no change needed
  30.     echo "$(date)   INFO! No change." >> ${__LOG__}
  31.     true;
  32. else
  33.     echo "$(date)   INFO! IP changed. Old: ${__PREVIOUS_IP__}. New: ${__IP__}. Attempting to update DNS." >> ${__LOG__}
  34.  
  35.     # Update DNS record
  36.     __QUERY_URL__="https://ssl.gratisdns.dk/ddns.phtml?u=${__USERNAME__}&p=${__PASSWORD__}&d=${__DOMAIN__}&h=${__HOSTNAME__}&i=${DDNS_CURRENT_IP}"
  37.     __QUERY_STATUS__=$(curl -s -k "${__QUERY_URL__}")
  38.     if [ "${__QUERY_STATUS__:0:2}" == "OK" ]; then
  39.         # Update IP got OK. Writing new IP to file
  40.         echo ${__IP__} > ${__IP_CACHE__}
  41.         echo "$(date)   INFO! IP successfully updated to ${__IP__}." >> ${__LOG__}
  42.     else
  43.         echo "$(date)   ERROR! Something unexpected happened when trying to update DNS. Status message from server: ${__QUERY_STATUS__}" >> ${__LOG__}
  44.     fi
  45. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement