Advertisement
tomearp

DDNS update client script

Feb 15th, 2013
617
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.81 KB | None | 0 0
  1. # python script to check for IP changes
  2. # and update a Free DNS account via the Direct URL method
  3. import syslog
  4. from urllib import urlopen
  5. from socket import getaddrinfo
  6.  
  7. # insert your Direct URL and DDNS host below
  8. checkurl = 'http://freedns.afraid.org/dynamic/check.php'
  9. directurl = 'https://thedirectupdateURLinyourFreeDNSaccount.com'
  10. ddnshost = 'hostname.you.selected.com'
  11.  
  12. def ddns_ip():
  13. # effectively perform a dns lookup
  14.     try:
  15.         ddnsip = getaddrinfo(ddnshost,'80')[0][4][0]
  16.         syslogwrite('Host ' + ddnshost + ' resolves to: ' + ddnsip)
  17.     except:
  18.         ddnsip = '0.0.0.0'
  19.         syslogwrite('DNS lookup failure')
  20.     return(ddnsip)
  21. # end ddns_ip
  22.  
  23. def actual_ip():
  24. # get ip detected by freedns website
  25.     try:
  26.         actualip = urlopen(checkurl).read().split()[3]
  27.         syslogwrite('External IP detected as: ' + actualip)
  28.     except:
  29.         actualip = '0.0.0.0'
  30.         syslogwrite('External IP detection failure')
  31.     return(actualip)
  32. # end actual_ip
  33.  
  34. def update_ip():
  35. # perform Direct URL update
  36.     try:
  37. # we're not interested in the server's response
  38.         dummy = urlopen(directurl).read()
  39.         syslogwrite('Updating Free DNS IP via Direct URL.')
  40.     except:
  41.         syslogwrite('Direct URL update failed.')
  42.     return 0
  43. # end update_ip
  44.  
  45. def syslogwrite(message):
  46.     syslog.syslog(syslog.LOG_INFO,message)
  47.     return 0
  48. # end syslogwrite
  49.  
  50. # main
  51. syslogwrite('Automatic update process started.')
  52. ddnsip = ddns_ip()
  53. actualip = actual_ip()
  54. # if lookup was successful
  55. if not ((ddnsip == '0.0.0.0') or (actualip == '0.0.0.0')):
  56. # if IPs don't match, carry out update
  57.     if not ddnsip == actualip:
  58.         update_ip()
  59. # else if lookup was not successful
  60. else:
  61.     syslogwrite('An error occured.')
  62. syslogwrite('Automatic update process completed.')
  63. # end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement