Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # install these python modules (pip install pif godaddypy)
- import logging
- import pif
- from godaddypy import Client, Account
- import socket
- # Original Source:
- # https://saschpe.wordpress.com/2013/11/12/godaddy-dyndns-for-the-poor/
- # http://blogs.umb.edu/michaelbazzinott001/2014/09/14/ddns-with-godaddy/
- # https://github.com/eXamadeus/godaddypy
- #
- # Modified by Jeremy Sears (https://stackoverflow.com/users/1240482/jsears)
- # Modified by Eric Gandt to support multiple names per domain and multiple domains plus short circuit when records match
- # Modified by Jason Frazier to use newest godaddypi and API key authentication instead of non-working pygodaddy U/P logins
- #
- # ensure you have already created yoursubdomainprefix.yourdomain.example as a new 'A' record in your GoDaddy DNS Management portal
- # ensure the time-to-live is reasonably short for DDNS-like operation, 600 seconds is enough without causing issues with GoDaddy
- # save this script in a location on your host, mark as executable and use crontab to execute every five minutes
- # */5 * * * * /PATH/TO/godaddy.py
- #
- # You must obtain a Production (not Test) GoDaddy API Key and secret for your account
- # https://developer.godaddy.com/keys
- #
- # you must define these one-letter-named variables and remove the @ signs
- K="@YOUR-GODADDY-API-KEY@"
- S="@YOUR-GODADDY-API-SECRET@"
- H="@yoursubdomainprefix@"
- D="@yourdomain.example@"
- L="@/PATH/TO/godaddy.log@"
- # nothing below here should need modification
- logging.basicConfig(filename=L, format='%(asctime)s %(message)s', level=logging.INFO)
- # the "requests" library logs noisily, so turn that off
- logging.getLogger("requests").setLevel(logging.DEBUG)
- logging.debug("DEBUG: Running godaddy_ddns.py");
- my_acct = Account(api_key=K, api_secret=S)
- client = Client(my_acct)
- public_ip = pif.get_public_ip()
- logging.debug("DEBUG: Current Public IP '{0}'.".format(public_ip))
- dns = socket.gethostbyname(H+"."+D)
- logging.debug("DEBUG: current DNS record for {0}.{1} is {2}.".format(H, D, dns))
- if public_ip != dns:
- # if the DNS and public IP differ then we may want to update the dns records
- for domain in client.get_domains():
- # since we may have multiple domain only return data for one of them
- if domain == D:
- # get the domain records
- logging.debug("DEBUG: Looking up DNS Records for {0}.".format(domain))
- dns_records = client.get_records(domain, record_type='A')
- # display what we actually got it is a list of 1 or more entries
- logging.debug("DEBUG: Domain '{0}' DNS records: {1}".format(domain, dns_records))
- if len(dns_records) == 0:
- logging.debug("DEBUG: No existing DNS records found for domain {0}.".format(domain))
- else:
- # we have DNS records, but we need to look at which record we actually want
- for dns_record in dns_records:
- logging.debug("DEBUG: Found existing record in DNS for {0} is '{1}'.".format(dns_record['name'], dns_record['data']))
- # compare the hostname to what we want to update
- if dns_record['name'] == H:
- logging.debug("DEBUG: Found desired record for {0} with an address of {1}.".format(H, dns_record['data']))
- # we do not need this check as tha was the first one executed, but to be safe do it again
- if dns_record['data'] == public_ip:
- # names match nothing needs to be done
- 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))
- else:
- # update is needed for the address
- 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))
- # finally call the function to make teh update to the DNS record, only update and include host and domain as provided by user
- success = client.update_record_ip(public_ip, D, H, 'A')
- if success:
- logging.info("INFO: Domain '{0}': Successfully set public IP to '{1}'.".format(domain, public_ip))
- else:
- logging.error("ERROR: Domain '{0}': Unable to update public IP to '{1}'.".format(domain, public_ip))
- # skip any remaining hosts
- break
- else:
- logging.debug("DEBUG: Skipping unwanted domain entry {0}.".format(dns_record['name']))
- # skip any remaining domains
- break
- else:
- logging.debug("DEBUG: Skipping unwanted Domain {0}.".format(domain))
- else:
- # short circuited exit as there was nothing we needed to do, and we did not have to query godaddy
- 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