dyndns 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #!/usr/bin/env python
  2. # http://blog.vrypan.net/2012/12/20/dynamic-dns-with-route53/
  3. import os
  4. import subprocess
  5. import sys
  6. from area53 import route53
  7. from boto.route53.exception import DNSServerError
  8. def get_ip():
  9. p = subprocess.Popen(['/usr/bin/dig',
  10. '+short',
  11. '@resolver1.opendns.com',
  12. 'myip.opendns.com'], stdout=subprocess.PIPE)
  13. output, err = p.communicate()
  14. if not err:
  15. return output.strip()
  16. else:
  17. sys.exit(1)
  18. def setup_env():
  19. access_key = subprocess.check_output(['/home/tyler/bin/getnetrc',
  20. 'duplicity', 'login'])
  21. key_id = subprocess.check_output(['/home/tyler/bin/getnetrc', 'duplicity'])
  22. os.environ['AWS_ACCESS_KEY_ID'] = key_id.strip()
  23. os.environ['AWS__SECRET_ACCESS_KEY'] = access_key.strip()
  24. def main():
  25. setup_env()
  26. sub_domain = 'home'
  27. domain = 'tylercipriani.com'
  28. fqdn = '%s.%s' % (sub_domain, domain)
  29. zone = route53.get_zone(domain)
  30. a_rec = zone.get_a(fqdn)
  31. new_ip = get_ip()
  32. if (a_rec):
  33. if new_ip == a_rec.resource_records[0]:
  34. print '%s is current. (%s)' % (fqdn, new_ip)
  35. sys.exit(0)
  36. try:
  37. zone.update_a(fqdn, new_ip, 900)
  38. except DNSServerError:
  39. zone.add_a(fqdn, new_ip, 900)
  40. else:
  41. zone.add_a(fqdn, new_ip, 900)
  42. if __name__ == '__main__':
  43. main()