def test_can_update_dns_record(): global client if client is None: client = GoDaddyClient() domain = account['test_domain'] prefix = 'test{}'.format(random.randint(10000, 99999)) addr = '{}.{}.{}.{}'.format(random.randint(10, 20), random.randint(10, 20), random.randint(10, 20), random.randint(10, 20)) addr2 = '{}.{}.{}.{}'.format(random.randint(20, 30), random.randint(20, 30), \ random.randint(20, 30), random.randint(20, 30)) # can create new record assert client.update_dns_record(prefix+'.'+domain, addr), 'Create DNS Failed' for record in client.find_dns_records(domain): if record.hostname == prefix: assert record.value == addr, 'DNS Record Doesnot Match Addr {}'.format(addr) break else: raise ValueError('DNS Record Cannot Created') # can update previous record assert client.update_dns_record(prefix+'.'+domain, addr2), 'Update DNS Failed' for record in client.find_dns_records(domain): if record.hostname == prefix: assert record.value == addr2, 'DNS Record Doesnot Match Addr2 {}'.format(addr2) break # can delete dns record assert client.delete_dns_record(prefix+'.'+domain), 'Delete DNS Failed' for record in client.find_dns_records(domain): if record.hostname == prefix: raise ValueError('DNS Record still exists')
def test_can_update_dns_record(): global client if client is None: client = GoDaddyClient() domain = account['test_domain'] prefix = 'test{}'.format(random.randint(10000, 99999)) addr = '{}.{}.{}.{}'.format(random.randint(10, 20), random.randint(10, 20), random.randint(10, 20), random.randint(10, 20)) addr2 = '{}.{}.{}.{}'.format(random.randint(20, 30), random.randint(20, 30), \ random.randint(20, 30), random.randint(20, 30)) # can create new record assert client.update_dns_record(prefix + '.' + domain, addr), 'Create DNS Failed' for record in client.find_dns_records(domain): if record.hostname == prefix: assert record.value == addr, 'DNS Record Doesnot Match Addr {}'.format( addr) break else: raise ValueError('DNS Record Cannot Created') # can update previous record assert client.update_dns_record(prefix + '.' + domain, addr2), 'Update DNS Failed' for record in client.find_dns_records(domain): if record.hostname == prefix: assert record.value == addr2, 'DNS Record Doesnot Match Addr2 {}'.format( addr2) break # can delete dns record assert client.delete_dns_record(prefix + '.' + domain), 'Delete DNS Failed' for record in client.find_dns_records(domain): if record.hostname == prefix: raise ValueError('DNS Record still exists')
#!/usr/bin/env python import logging import pif from pygodaddy import GoDaddyClient logging.basicConfig(filename='godaddy.log', format='%(asctime)s %(message)s', level=logging.INFO) GODADDY_USERNAME="******"; GODADDY_PASSWORD="******"; client = GoDaddyClient() client.login(GODADDY_USERNAME, GODADDY_PASSWORD) logging.info(client.find_domains()) for domain in client.find_domains(): dns_records = client.find_dns_records(domain) public_ip = pif.get_public_ip() logging.debug("Domain '{0}' DNS records: {1}".format(domain, dns_records)) if public_ip != dns_records[0].value: client.update_dns_record(domain, public_ip) logging.info("Domain '{0}' public IP set to '{1}'".format(domain, public_ip))
class GodaddyDnsUpdater: @property def __publicip(self): return get('https://api.ipify.org').text @property def __isfullymanaged(self): if self.__configparser.get('global', 'fully_managed').lower() == 'yes': return True else: return False @property def __usetls(self): if self.__configparser.get('global', 'smtp_tls').lower() == 'yes': return True else: return False def __init__(self, configfile): self.__godaddyclient = GoDaddyClient() # read config self.__configparser = ConfigParser() self.__configparser.read(configfile) self.__updatemessage = '' self.__username = self.__configparser.get('global', 'username') self.__password = self.__configparser.get('global', 'password') self.__logfile = self.__configparser.get('global', 'logfile') self.__changedetected = False self.__domains = [] for section in self.__configparser.sections(): if section != 'global': self.__domains.append(section) # mail config self.__mailnotifyenabled = self.__configparser.get('global', 'mail_notify_enabled') self.__mailreceiver = self.__configparser.get('global', 'mail_receiver') self.__mailsender = self.__configparser.get('global', 'mail_sender') self.__mailuser = self.__configparser.get('global', 'smtp_user') self.__mailpassword = self.__configparser.get('global', 'smtp_password') self.__smtphost = self.__configparser.get('global', 'smtp_host') self.__smtpport = self.__configparser.get('global', 'smtp_port') def updatedns(self): self.__login() for domain in self.__domains: self.__update(domain) if self.__mailnotifyenabled and self.__changedetected: self.__mailnotify(self.__updatemessage) def __update(self, domain): hostnames = [hostname.strip() for hostname in self.__configparser.get(domain, 'hostnames').split(',')] godaddyhostnames = [host[1] for host in self.__godaddyclient.find_dns_records(domain)] # update listed hostnames for hostname in hostnames: if hostname in godaddyhostnames: if self.__godaddyclient.update_dns_record(hostname + '.' + domain, self.__publicip): print(hostname + '.' + domain + ' updated.') self.__changedetected = True self.__updatemessage = self.__updatemessage + hostname + '.' + domain + ' updated. \n' else: if self.__isfullymanaged: if self.__godaddyclient.update_dns_record(hostname + '.' + domain, self.__publicip): print(hostname + '.' + domain + ' added.') self.__changedetected = True self.__updatemessage = self.__updatemessage + hostname + '.' + domain + ' added. \n' # delete not listed hostnames godaddyhostnames = [host[1] for host in self.__godaddyclient.find_dns_records(domain)] if self.__isfullymanaged: for godaddyhostname in godaddyhostnames: if godaddyhostname not in hostnames: if self.__godaddyclient.delete_dns_record(godaddyhostname + '.' + domain): print(godaddyhostname + '.' + domain + ' deleted.') self.__changedetected = True self.__updatemessage = self.__updatemessage + godaddyhostname + '.' + domain + ' deleted. \n' def __login(self): if not self.__godaddyclient.is_loggedin(): self.__godaddyclient.login(self.__username, self.__password) def __mailnotify(self, message): smtpserver = SMTP(self.__smtphost, self.__smtpport) header = 'To:' + self.__mailreceiver + '\n' + 'From: ' + self.__mailsender + '\n' + 'Subject:Godaddy dns updater \n' msg = header + '\n' + 'Public ip: ' + self.__publicip + '\n\n' + message + '\n\n' try: if self.__usetls: smtpserver.starttls() smtpserver.login(self.__mailuser, self.__mailpassword) smtpserver.sendmail(self.__mailsender, self.__mailreceiver, msg) print('mail notify sent.') except: print('mail notify failed service failed!') finally: smtpserver.close()
#what is my public ip? public_ip = pif.get_public_ip() logging.info("My ip: {0}".format(public_ip)) # login to GoDaddy DNS management # docs at https://pygodaddy.readthedocs.org/en/latest/ client = GoDaddyClient() if client.login(godaddy.gduser, godaddy.gdpass): # find out current dns record value. This can also be done with a quick nslookupA # with something like socket.gethostbyname() # however, this can include caching somewhere in the dns layers # We could also use dnspython libraray, but that adds a lot of complexity # use the GoDaddy object to find our current IP registered domaininfo = client.find_dns_records(godaddy.domain) for record in domaininfo: if record.hostname == godaddy.host: if record.value != public_ip: logging.info("Update required: old {0}, new {1}".format(record.value, public_ip)) updateinfo = "old " + record.value + ", new " + public_ip # This will fail if you try to set the same IP as already registered! if client.update_dns_record(godaddy.host+"."+godaddy.domain, public_ip): logging.info('Update OK') email_update("Update OK!\n"+updateinfo) else: logging.info('Update DNS FAILED!') email_update("Update failed!\n"+updateinfo) else: logging.info('No update required.')
GODADDY_USERNAME="******" GODADDY_PASSWORD="******" client = GoDaddyClient() domain = 'domain' public_ip = pif.get_public_ip() # this file contains current ip address of your server ip_file_value = open('/path/to/ip_file', 'r').read() if ip_file_value == public_ip: exit(0) else: if client.login(GODADDY_USERNAME, GODADDY_PASSWORD): hostnames = [] # update IP for every A record for i in range(len(client.find_dns_records(domain))): hostnames.append(client.find_dns_records(domain)[i].hostname) client.update_dns_record(hostnames[i]+"."+domain, public_ip) logging.info("Success! IP = '{0}'".format(public_ip)) # write a new IP to file ip_file = open('/path/to/ip_file', 'w') ip_file.write(public_ip) ip_file.close() else: logging.info("Login failed".format())