diff --git a/README.md b/README.md index 1564078d87e0d640e04698da9e859787064587b3..d971c40778824524f8b464b0701dc214c7966322 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,6 @@ Simple Python script to update DNS A record of your domain dynamically using gan http://doc.livedns.gandi.net/ -The config-example.txt file should be renamed to config.txt, and modified with your gandi.net API key, domain name, and A-record (@, dev, home, pi, etc). +The config-template.txt file should be renamed to config.txt, and modified with your gandi.net API key, domain name, and A-record (@, dev, home, pi, etc). Every time the script runs, it will query an external service to retrieve the external IP of the machine, compare it to the current record (if any) in the zone at gandi.net, and then add a new record (if none currently exists), or delete then add a new record (if a record already exists). diff --git a/gandi.py b/gandi-ddns.py similarity index 63% rename from gandi.py rename to gandi-ddns.py index 000fd0a01f7167e1ff1eb3cd5bb2811a1549bd1b..27d806d32d40b5a999932c837bf95fea80a9c35a 100644 --- a/gandi.py +++ b/gandi-ddns.py @@ -69,39 +69,35 @@ def main(): sys.exit("please fill in the 'config.txt' file") for section in config.sections(): - print(config.get(section, "api")) - print(config.get(section, "domain")) - print(config.get(section, "a_name")) - print(config.get(section, "ttl")) - #Retrieve API key - apikey = config.get(section, "apikey") - - #Set headers - headers = { 'Content-Type': 'application/json', 'X-Api-Key': '%s' % config.get(section, "apikey")} - - #Set URL - url = 'https://dns.beta.gandi.net/api/v5/domains/%s/records/%s/A' % (config.get(section, "domain"), config.get(section, "a_name")) - - #Discover External IP - external_ip = get_ip()[0:-1] - print("external IP is: %s" % external_ip) - - #Prepare record - payload = {'rrset_ttl': 900, 'rrset_values': [external_ip]} - - #Check if record already exists - record = get_record(url, headers) - - if record.status_code == 404: - add_record(url, headers, payload) - elif record.status_code == 200: - print("current record is: %s" % json.loads(record.text)['rrset_values'][0]) - if(json.loads(record.text)['rrset_values'][0] == external_ip): - print("no change in IP address") - sys.exit(2) - del_record(url, headers) - add_record(url, headers, payload) + #Retrieve API key + apikey = config.get(section, "apikey") + + #Set headers + headers = { 'Content-Type': 'application/json', 'X-Api-Key': '%s' % config.get(section, "apikey")} + + #Set URL + url = '%sdomains/%s/records/%s/A' % (config.get(section, "api"), config.get(section, "domain"), config.get(section, "a_name")) + print(url) + #Discover External IP + external_ip = get_ip()[0:-1] + print("external IP is: %s" % external_ip) + + #Prepare record + payload = {'rrset_ttl': 900, 'rrset_values': [external_ip]} + + #Check if record already exists. If not, add record. If it does, delete then add record. + record = get_record(url, headers) + + if record.status_code == 404: + add_record(url, headers, payload) + elif record.status_code == 200: + print("current record is: %s" % json.loads(record.text)['rrset_values'][0]) + if(json.loads(record.text)['rrset_values'][0] == external_ip): + print("no change in IP address") + sys.exit(2) + del_record(url, headers) + add_record(url, headers, payload) if __name__ == "__main__": main()