def godaddycli(username, password, cfg): client = GoDaddyClient() c = client.login(username, password) if not c: print "couldn't login" sys.exit(1) if cfg.delete is not None: godaddycli_delete(client, cfg) return 0 if cfg.update is not None: godaddycli_update(client, cfg) return 0 if cfg.list is not None: godaddycli_list(client, cfg) return 0
#!/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()
for domain in domains: realdomain = domain if realdomain.startswith("@."): realdomain = realdomain[2:] dom_ip = socket.gethostbyname(realdomain) if wan_ip != dom_ip: print '----------------------------------------------------' print 'GoDaddy A Record Update | ' + realdomain + ' (' + domain + ') | ' + time.strftime( "%a %d %B %I:%M %p") print 'WAN IP:\t\t' + wan_ip print 'Domain IP:\t' + dom_ip print 'IPs don\'t match, checking if already updated...' old_ip = '0' if os.path.isfile("/usr/local/etc/old_ips/" + realdomain): oipf = open("/usr/local/etc/old_ips/" + realdomain) old_ip = oipf.read() print 'Old IP:\t\t' + old_ip if wan_ip != old_ip: print 'Updating A Record...' client = GoDaddyClient() if client.login(username, password): client.update_dns_record(domain, wan_ip) nipf = open("/usr/local/etc/old_ips/" + realdomain, 'w+') nipf.write(wan_ip) nipf.close() print 'DNS has been updated.' else: print 'Failed to login, please check \'update-dns.py\'' else: print 'Already updated, dns not flushed.'
for domain in domains: realdomain = domain if realdomain.startswith("@."): realdomain = realdomain[2:] dom_ip = socket.gethostbyname(realdomain) if wan_ip != dom_ip: print '----------------------------------------------------' print 'GoDaddy A Record Update | ' + realdomain + ' (' + domain + ') | ' + time.strftime("%a %d %B %I:%M %p") print 'WAN IP:\t\t' + wan_ip print 'Domain IP:\t' + dom_ip print 'IPs don\'t match, checking if already updated...' old_ip = '0' if os.path.isfile("/usr/local/etc/old_ips/" + realdomain): oipf = open("/usr/local/etc/old_ips/" + realdomain) old_ip = oipf.read() print 'Old IP:\t\t' + old_ip if wan_ip != old_ip: print 'Updating A Record...' client = GoDaddyClient() if client.login(username, password): client.update_dns_record(domain, wan_ip) nipf = open("/usr/local/etc/old_ips/" + realdomain, 'w+') nipf.write(wan_ip) nipf.close() print 'DNS has been updated.' else: print 'Failed to login, please check \'update-dns.py\'' else: print 'Already updated, dns not flushed.'
def test_login(): global client if client is None: client = GoDaddyClient() assert client.login(account['username'], account['password']), 'Login Failed'
parser = argparse.ArgumentParser('A Python script to do updates to a GoDaddy DNS host A record') parser.add_argument('-v', '--verbose', action='store_true', help="send emails on 'no ip update required'") args = parser.parse_args() #start log file logging.basicConfig(filename=godaddy.logfile, format='%(asctime)s %(message)s', level=logging.INFO) #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')