From b9a7e2bed5125737d71e06594c01bf2899f80a82 Mon Sep 17 00:00:00 2001 From: Daniel Dammermann Date: Thu, 2 May 2019 23:18:37 +0200 Subject: [PATCH 1/2] support for getting the ip via bash command --- src/example.config.py | 8 ++++++++ src/gandi-live-dns.py | 9 ++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/example.config.py b/src/example.config.py index 7ac37fc..6091a45 100644 --- a/src/example.config.py +++ b/src/example.config.py @@ -43,3 +43,11 @@ + many more ... ''' ifconfig = 'choose_from_above_or_run_your_own' + +''' +A bash command to run in order to retrieve the current IPv4. +If set, takes precedence over 'ifconfig'. + +The command used should only output the ip address with no additional output! +''' +ipcommand = '' diff --git a/src/gandi-live-dns.py b/src/gandi-live-dns.py index 55e5757..73d3fd3 100755 --- a/src/gandi-live-dns.py +++ b/src/gandi-live-dns.py @@ -15,7 +15,14 @@ import requests, json import config import argparse +import subprocess +def get_dynip_cmd(ip_command): + ''' find out own IPv4 at home <-- this is the dynamic IP which changes more or less frequently + e.g., "curl ifconfig.me/ip", see example.config.py for details to ipcommand providers + ''' + ip = subprocess.Popen(ip_command.split(), stdout=subprocess.PIPE).communicate()[0] + return ip.strip('\n') def get_dynip(ifconfig_provider): ''' find out own IPv4 at home <-- this is the dynamic IP which changes more or less frequently @@ -99,7 +106,7 @@ def main(force_update, verbosity): uuid = get_uuid() #compare dynIP and DNS IP - dynIP = get_dynip(config.ifconfig) + dynIP = get_dynip_cmd(config.ipcommand) if config.ipcommand else get_dynip(config.ifconfig) dnsIP = get_dnsip(uuid) if force_update: From 967d6a12335ebdf902db34b75cd7c882eb4394b6 Mon Sep 17 00:00:00 2001 From: Daniel Dammermann Date: Sat, 18 Feb 2023 01:12:40 +0100 Subject: [PATCH 2/2] python 3 --- src/gandi-live-dns.py | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/gandi-live-dns.py b/src/gandi-live-dns.py index 73d3fd3..99fe086 100755 --- a/src/gandi-live-dns.py +++ b/src/gandi-live-dns.py @@ -21,7 +21,7 @@ def get_dynip_cmd(ip_command): ''' find out own IPv4 at home <-- this is the dynamic IP which changes more or less frequently e.g., "curl ifconfig.me/ip", see example.config.py for details to ipcommand providers ''' - ip = subprocess.Popen(ip_command.split(), stdout=subprocess.PIPE).communicate()[0] + ip = subprocess.Popen(ip_command.split(), stdout=subprocess.PIPE).communicate()[0].decode() return ip.strip('\n') def get_dynip(ifconfig_provider): @@ -29,7 +29,7 @@ def get_dynip(ifconfig_provider): similar to curl ifconfig.me/ip, see example.config.py for details to ifconfig providers ''' r = requests.get(ifconfig_provider) - print 'Checking dynamic IP: ' , r._content.strip('\n') + print('Checking dynamic IP: ' , r._content.strip('\n')) return r.content.strip('\n') def get_uuid(): @@ -45,8 +45,8 @@ def get_uuid(): 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): @@ -63,11 +63,12 @@ def get_dnsip(uuid): 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(json.dumps(json_object, indent=2)) + print('Got 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): @@ -87,11 +88,11 @@ def update_records(uuid, dynIP, subdomain): 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() @@ -99,7 +100,7 @@ def update_records(uuid, dynIP, subdomain): def main(force_update, verbosity): if verbosity: - print "verbosity turned on - not implemented by now" + print("verbosity turned on - not implemented by now") #get zone ID from Account @@ -110,14 +111,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)