Advertisement
johnmahugu

Quick and dirty DynDNS client in python

Jul 20th, 2016
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. """
  2. Quick and dirty DynDNS client in python.
  3. Published  on January 13, 2016 (queries: johnmahugu at gmail dot com)
  4. I needed a client for my dyndns account to run on Linux. I could of course use something like ddclient which I’m sure would work great, but the API is so simple it seems like a waste to use a 4500+ line perl script to do it. Instead I just wrote up a quick and dirty Python + Requests script which works great. I thought I’d share it.
  5.  
  6. The error messaging isn’t very pythonic, and there’s no logging which would be nice, but I popped it in my /etc/cron.hourly folder and it works great.
  7. """
  8. #!/usr/bin/python
  9. import requests
  10. import json
  11.  
  12. user = "your user name here"
  13. password = "your api code here (or password)"
  14. checkip = "http://thisisnt.com/api/getRemoteIp.php"
  15. dynupdate = "https://members.dyndns.com/nic/update"
  16.  
  17.  
  18.  
  19. print "starting. Get current IP..."
  20. ipraw = requests.get(checkip)
  21. if ipraw.status_code is not 200:
  22.   raise "Cannot get IP address"
  23.   exit
  24.  
  25. ip = ipraw.json()['REMOTE_ADDR']
  26. print "Remote IP: " + ip
  27. print "updating..."
  28.  
  29. # update dyndns
  30. headers = {'user-agent': 'mPythonClient/0.0.3'}
  31. dyn = requests.get(dynupdate, \
  32.               headers=headers, \
  33.               auth=(user, password), \
  34.               params={'hostname': '<domain name to update>', \
  35.                        'myip': ip, \
  36.                        'wildcard': 'NOCHG', \
  37.                        'mx': '<mx host for domain>', \
  38.                        })
  39.  
  40. if dyn.status_code is not 200:
  41.   print "Update failed. HTTP Code: " + str(dyn.status_code)
  42. if "good" in dyn.text:
  43.   print "update successful.."
  44. else:
  45.   print "Update unsuccessful: " + dyn.text.strip()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement