Advertisement
briped

GratisDNS.sh

Mar 3rd, 2012
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/sh
  2.  
  3. # User Configuration
  4. __USERNAME__="MyUserName"
  5. __PASSWORD__="MyPassword"
  6. __HOSTNAME__="host.example.com"
  7. __DOMAIN__=$(echo ${__HOSTNAME__} | cut -d. -f2-3)
  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. __LOGFILE__=/var/log/gratisdns_ddns.log
  14.  
  15.  
  16. log() {
  17.     __LOGTIME__=$(date +"%b %e %T")
  18.     if [ "${#}" -lt 1 ]; then
  19.         false
  20.     else
  21.         __LOGMSG__="${1}"
  22.     fi
  23.     if [ "${#}" -lt 2 ]; then
  24.         __LOGPRIO__=7
  25.     else
  26.         __LOGPRIO__=${2}
  27.     fi
  28.  
  29.     #logger -p ${__LOGPRIO__} -t "$(basename ${0})" "${__LOGMSG__}"
  30.     echo "${__LOGTIME__} $(basename ${0}) (${__LOGPRIO__}): ${__LOGMSG__}" >> ${__LOGFILE__}
  31. }
  32.  
  33. # Get the last known DDNS IP
  34. if [ ! -f ${__IP_CACHE__} ]; then
  35.     echo "127.0.0.1" > ${__IP_CACHE__}
  36. fi
  37. __OLDIP__=$(cat ${__IP_CACHE__})
  38.  
  39. if [ -z "${__INTERFACE__}" ]; then
  40.     # Interface not defined, so getting the public IP through external lookup.
  41.     __MYIP__=$(curl -s "http://automation.whatismyip.com/n09230945.asp")
  42. else
  43.     # Get the current IP for the specified interface.
  44.     __MYIP__=$(ifconfig ${__INTERFACE__} | sed '/inet\ /!d;s/.*r://g;s/\ .*//g')
  45. fi
  46.  
  47. if [ "${__OLDIP__}" == "${__MYIP__}" ]; then
  48.     log "IP not changed. ${__MYIP__}. Not updating." 7
  49.     true
  50. else
  51.     log "IP changed. ${__OLDIP__} > ${__MYIP__}. Attempting to update DNS." 6
  52.  
  53.     # Update DNS record
  54.     __URL__="https://ssl.gratisdns.dk/ddns.phtml?u=${__USERNAME__}&p=${__PASSWORD__}&d=${__DOMAIN__}&h=${__HOSTNAME__}&i=${__MYIP__}"
  55.     __STATUS__=$(curl -s -k "${__URL__}")
  56.     case ${__STATUS__} in
  57.         'OK<br>')
  58.             echo ${__MYIP__} > ${__IP_CACHE__}
  59.             log "IP successfully updated." 6
  60.             true
  61.             ;;
  62.         'OK<br>Opdateret i forvejen')
  63.             echo ${__MYIP__} > ${__IP_CACHE__}
  64.             log "IP successfully updated. However, the IP was already updated, according to the DDNS provider." 5
  65.             true
  66.             ;;
  67.         'Forkerte værdier, opdatering kan ikke laves.<br><br>A record findes ikke.')
  68.             log "Invalid parameters, nothing was updated. The A record '${__HOSTNAME__}' doesn't exist." 3
  69.             false
  70.             ;;
  71.         'Domæne kan IKKE administreres af bruger')
  72.             log "You do not have permission to update DDNS for the domain '${__DOMAIN__}'." 3
  73.             false
  74.             ;;
  75.         'Forkerte værdier, opdatering kan ikke laves.<br><br>A record findes ikke.Hostnavn er ulovligt.')
  76.             log "Invalid parameters, nothing was updated. The A record for '${__HOSTNAME__}' doesn't exist. The hostname must be a FQDN." 3
  77.             false
  78.             ;;
  79.         'Bruger login: 1Fejl i kodeord, prøv igen. Husk serveren ser forskel på STORE Og små BOGstAvER.')
  80.             log "Incorrect password for '${__USERNAME__}'. The password is CaSe SeNsItIvE." 3
  81.             false
  82.             ;;
  83.         'Bruger login: Bruger eksistere ikke, husk serveren ser forskel på STORE Og smÅ BOGstAvER.')
  84.             log "The supplied username '${__USERNAME__}' wasn't found. The username is CaSe SeNsItIvE." 3
  85.             false
  86.             ;;
  87.     esac
  88. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement