diff --git a/README.md b/README.md index 70fc261..70c6231 100644 --- a/README.md +++ b/README.md @@ -112,8 +112,8 @@ root@laptop:~# curl https://blog.cavebeat.org/ip/ ``` This should fit your personal needs and you still selfhost the whole thing. -#### IP address lookup service https://ifconfig.co -https://github.com/mpolden/ipd A simple service for looking up your IP address. This is the code that powers [https://ifconfig.co](https://ifconfig.co) +#### IP address lookup service https://ip4.me/ip/ +https://github.com/mpolden/ipd A simple service for looking up your IP address. This is the code that powers [https://ip4.me/ip/](https://ip4.me/ip/) #### use external services choose one as described in the config file. diff --git a/src/.gitignore b/src/.gitignore index 3abc68b..558c887 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -1,2 +1,3 @@ /config.py /test.py +__pycache__/ diff --git a/src/example.config.py b/src/example.config.py index 7ac37fc..02a9321 100644 --- a/src/example.config.py +++ b/src/example.config.py @@ -36,7 +36,7 @@ + e.g. -+ https://ifconfig.co/ip ++ https://ip4.me/ip/ + http://ifconfig.me/ip + http://whatismyip.akamai.com/ + http://ipinfo.io/ip diff --git a/src/gandi-live-dns.py b/src/gandi-live-dns.py index 55e5757..dbd4112 100755 --- a/src/gandi-live-dns.py +++ b/src/gandi-live-dns.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # encoding: utf-8 ''' Gandi v5 LiveDNS - DynDNS Update via REST API and CURL/requests @@ -21,9 +21,11 @@ def get_dynip(ifconfig_provider): ''' find out own IPv4 at home <-- this is the dynamic IP which changes more or less frequently similar to curl ifconfig.me/ip, see example.config.py for details to ifconfig providers ''' + if config.verbosity: + print('url get: %s' % (ifconfig_provider)) r = requests.get(ifconfig_provider) - print 'Checking dynamic IP: ' , r._content.strip('\n') - return r.content.strip('\n') + print('Checking dynamic IP: ' , r._content.decode('utf-8').strip('\n')) + return r.content.decode('utf-8').strip('\n') def get_uuid(): ''' @@ -33,13 +35,15 @@ def get_uuid(): ''' url = config.api_endpoint + '/domains/' + config.domain + if config.verbosity: + print('url get: %s' % (url)) u = requests.get(url, headers={"X-Api-Key":config.api_secret}) json_object = json.loads(u._content) if u.status_code == 200: return json_object['zone_uuid'] else: - print 'Error: HTTP Status Code ', u.status_code, 'when trying to get Zone UUID' - print json_object['message'] + print('Error: HTTP Status Code ', u.status_code, 'when trying to get Zone UUID') + print(json_object['message']) exit() def get_dnsip(uuid): @@ -51,16 +55,18 @@ def get_dnsip(uuid): the actual DNS Record IP ''' - url = config.api_endpoint+ '/zones/' + uuid + '/records/' + config.subdomains[0] + '/A' + url = config.api_endpoint+ '/domains/' + config.domain + '/records/' + config.subdomains[0] + '/A' headers = {"X-Api-Key":config.api_secret} + if config.verbosity: + print('url get: %s' % (url)) u = requests.get(url, headers=headers) if u.status_code == 200: json_object = json.loads(u._content) - print 'Checking IP from DNS Record' , config.subdomains[0], ':', json_object['rrset_values'][0].encode('ascii','ignore').strip('\n') - return json_object['rrset_values'][0].encode('ascii','ignore').strip('\n') + print('Checking IP from DNS Record' , config.subdomains[0], ':', json_object['rrset_values'][0].strip('\n')) + return json_object['rrset_values'][0].strip('\n') else: - print 'Error: HTTP Status Code ', u.status_code, 'when trying to get IP from subdomain', config.subdomains[0] - print json_object['message'] + print('Error: HTTP Status Code ', u.status_code, 'when trying to get IP from subdomain', config.subdomains[0]) + print(json_object['message']) exit() def update_records(uuid, dynIP, subdomain): @@ -76,23 +82,26 @@ def update_records(uuid, dynIP, subdomain): url = config.api_endpoint+ '/zones/' + uuid + '/records/' + subdomain + '/A' payload = {"rrset_ttl": config.ttl, "rrset_values": [dynIP]} headers = {"Content-Type": "application/json", "X-Api-Key":config.api_secret} + if config.verbosity: + print('url put: %s. payload: %s' % (url, json.dumps(payload))) u = requests.put(url, data=json.dumps(payload), headers=headers) json_object = json.loads(u._content) if u.status_code == 201: - print 'Status Code:', u.status_code, ',', json_object['message'], ', IP updated for', subdomain + print('Status Code:', u.status_code, ',', json_object['message'], ', IP updated for', subdomain) return True else: - print 'Error: HTTP Status Code ', u.status_code, 'when trying to update IP from subdomain', subdomain - print json_object['message'] + print('Error: HTTP Status Code ', u.status_code, 'when trying to update IP from subdomain', subdomain) + print(json_object['message']) exit() def main(force_update, verbosity): + config.verbosity = verbosity if verbosity: - print "verbosity turned on - not implemented by now" + print("verbosity turned on - not implemented by now") #get zone ID from Account @@ -103,14 +112,14 @@ def main(force_update, verbosity): dnsIP = get_dnsip(uuid) if force_update: - print "Going to update/create the DNS Records for the subdomains" + print("Going to update/create the DNS Records for the subdomains") for sub in config.subdomains: update_records(uuid, dynIP, sub) else: if dynIP == dnsIP: - print "IP Address Match - no further action" + print("IP Address Match - no further action") else: - print "IP Address Mismatch - going to update the DNS Records for the subdomains with new IP", dynIP + print("IP Address Mismatch - going to update the DNS Records for the subdomains with new IP", dynIP) for sub in config.subdomains: update_records(uuid, dynIP, sub)