From 7c6fe2add7c2fbec5416b665d0997a85247ae191 Mon Sep 17 00:00:00 2001 From: arjuan <arjuan@raspberrypi> Date: Mon, 3 Apr 2017 23:08:39 +0000 Subject: [PATCH] typos and clarifications --- README.md | 2 +- gandi.py => gandi-ddns.py | 60 ++++++++++++++++++--------------------- 2 files changed, 29 insertions(+), 33 deletions(-) rename gandi.py => gandi-ddns.py (63%) diff --git a/README.md b/README.md index 1564078..d971c40 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 000fd0a..27d806d 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() -- GitLab