Advertisement
neveroddoreven

GoDaddy DNS Host subdomain updater

Mar 19th, 2017
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.15 KB | None | 0 0
  1. #!/usr/bin/env python
  2. # install these python modules (pip install pif godaddypy)
  3. import logging
  4. import pif
  5. from godaddypy import Client, Account
  6. import socket
  7.  
  8. # Original Source:
  9. # https://saschpe.wordpress.com/2013/11/12/godaddy-dyndns-for-the-poor/
  10. # http://blogs.umb.edu/michaelbazzinott001/2014/09/14/ddns-with-godaddy/
  11. # https://github.com/eXamadeus/godaddypy
  12. #
  13. # Modified by Jeremy Sears (https://stackoverflow.com/users/1240482/jsears)
  14. # Modified by Eric Gandt to support multiple names per domain and multiple domains plus short circuit when records match
  15. # Modified by Jason Frazier to use newest godaddypi and API key authentication instead of non-working pygodaddy U/P logins
  16. #
  17. # ensure you have already created yoursubdomainprefix.yourdomain.example as a new 'A' record in your GoDaddy DNS Management portal
  18. # ensure the time-to-live is reasonably short for DDNS-like operation, 600 seconds is enough without causing issues with GoDaddy
  19. # save this script in a location on your host, mark as executable and use crontab to execute every five minutes
  20. # */5 * * * * /PATH/TO/godaddy.py
  21. #
  22. # You must obtain a Production (not Test) GoDaddy API Key and secret for your account
  23. # https://developer.godaddy.com/keys
  24. #
  25. # you must define these one-letter-named variables and remove the @ signs
  26. K="@YOUR-GODADDY-API-KEY@"
  27. S="@YOUR-GODADDY-API-SECRET@"
  28. H="@yoursubdomainprefix@"
  29. D="@yourdomain.example@"
  30. L="@/PATH/TO/godaddy.log@"
  31.  
  32. # nothing below here should need modification
  33. logging.basicConfig(filename=L, format='%(asctime)s %(message)s', level=logging.INFO)
  34. # the "requests" library logs noisily, so turn that off
  35. logging.getLogger("requests").setLevel(logging.DEBUG)
  36. logging.debug("DEBUG: Running godaddy_ddns.py");
  37. my_acct = Account(api_key=K, api_secret=S)
  38. client = Client(my_acct)
  39.  
  40. public_ip = pif.get_public_ip()
  41. logging.debug("DEBUG: Current Public IP '{0}'.".format(public_ip))
  42. dns = socket.gethostbyname(H+"."+D)
  43. logging.debug("DEBUG: current DNS record for {0}.{1} is {2}.".format(H, D, dns))
  44. if public_ip != dns:
  45.     # if the DNS and public IP differ then we may want to update the dns records
  46.     for domain in client.get_domains():
  47.         # since we may have multiple domain only return data for one of them
  48.         if domain == D:
  49.             # get the domain records
  50.             logging.debug("DEBUG: Looking up DNS Records for {0}.".format(domain))
  51.             dns_records = client.get_records(domain, record_type='A')
  52.             # display what we actually got it is a list of 1 or more entries
  53.             logging.debug("DEBUG: Domain '{0}' DNS records: {1}".format(domain, dns_records))
  54.             if len(dns_records) == 0:
  55.                 logging.debug("DEBUG: No existing DNS records found for domain {0}.".format(domain))
  56.             else:
  57.                 # we have DNS records, but we need to look at which record we actually want
  58.                 for dns_record in dns_records:
  59.                     logging.debug("DEBUG: Found existing record in DNS for {0} is '{1}'.".format(dns_record['name'], dns_record['data']))
  60.                     # compare the hostname to what we want to update
  61.                     if dns_record['name'] == H:
  62.                         logging.debug("DEBUG: Found desired record for {0} with an address of {1}.".format(H, dns_record['data']))
  63.                         # we do not need this check as tha was the first one executed, but to be safe do it again
  64.                         if dns_record['data'] == public_ip:
  65.                             # names match nothing needs to be done
  66.                             logging.info("INFO: Public IP A record DNS record for host '{0}' is up to date, and does not need to be updated.".format(H))
  67.                         else:
  68.                             # update is needed for the address
  69.                             logging.info("INFO: Updating A record for {0} ({1}) from address {2} to {3}.".format(dns_record['name'], H+"."+D, dns_record['data'], public_ip))
  70.                             # finally call the function to make teh update to the DNS record, only update and include host and domain as provided by user
  71.                             success = client.update_record_ip(public_ip, D, H, 'A')
  72.                             if success:
  73.                                 logging.info("INFO: Domain '{0}': Successfully set public IP to '{1}'.".format(domain, public_ip))
  74.                             else:
  75.                                 logging.error("ERROR: Domain '{0}': Unable to update public IP to '{1}'.".format(domain, public_ip))
  76.                                 # skip any remaining hosts
  77.                                 break
  78.                     else:
  79.                         logging.debug("DEBUG: Skipping unwanted domain entry {0}.".format(dns_record['name']))
  80.                 # skip any remaining domains
  81.                 break
  82.         else:
  83.             logging.debug("DEBUG: Skipping unwanted Domain {0}.".format(domain))
  84. else:
  85.     # short circuited exit as there was nothing we needed to do, and we did not have to query godaddy
  86.     logging.info("INFO: current DNS record for {0}.{1} is {2} which matches the discovered address of {3}, nothing to do.".format(H, D, dns, public_ip))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement