From 927f12abfffdfaf2f797734fe6c1b44064877824 Mon Sep 17 00:00:00 2001 From: Steve Date: Mon, 24 Jul 2023 19:25:47 -0400 Subject: [PATCH] more fixes, use icanhaz --- src/__pycache__/config.cpython-311.pyc | Bin 0 -> 1397 bytes src/config.py | 4 +- src/gandi-live-dns.py | 6 +- src/gandi-live-dns.py.bak | 130 +++++++++++++++++++++++++ 4 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 src/__pycache__/config.cpython-311.pyc create mode 100755 src/gandi-live-dns.py.bak diff --git a/src/__pycache__/config.cpython-311.pyc b/src/__pycache__/config.cpython-311.pyc new file mode 100644 index 0000000000000000000000000000000000000000..6cb5a8f4534754e88b033806f2ff5b2e13dc3426 GIT binary patch literal 1397 zcmbV~Pe|KP9LE!D)27L5oXQT)m9d-uno>6?{=?2z$)>5;)I$%Jnip%NCMAi2JxCdY zK`C^%?KFCF4lf>io5vlK5@>lRVW-^&-n?w7oo>z@itj;q@Av)vKJSy(a?1k>z zdFkFR=mhlKuIifZ?tF}O-gHkFbl)Ai_rJbVN6rnN+}vWC?1D{0a8K8L zUC{lIxhI{1coG}w``-Rh9`!9n`52dpu5y`Vd|@pvAh(cs73VMUrwBv+f7Cc1TTO7h zP<*m&7zKTbWuc-6vjsVr%PLR~Dw4rEJ9hs$VmehxAu4bS8EzHPoj@$H66J1=Or~CR z0?A8!9Dz=Xz^%j+DFL}ALm}kv#JI(U1i#2F6(3%oEQ?uXJ6n_sK~ddhM`+Ze>c}H0 z`?97g$d!`}Fy%9fn_j9s*Sy9~42>aN7z0A~ZPI-tlP|bli6r68)il{;g-eZY$jKt z&uM?Rfrw92zyp7Az`eQ?Vws+@O}f3g((%Q7M~BmAvh# I4Ldgd0vM4lKL7v# literal 0 HcmV?d00001 diff --git a/src/config.py b/src/config.py index f4eb9c9..ea0f691 100644 --- a/src/config.py +++ b/src/config.py @@ -5,6 +5,6 @@ api_endpoint = os.environ.get("GANDI_API_ENDPOINT",'https://dns.api.gandi.net/ap static_ip = os.environ.get("GANDI_STATIC_IP","").replace("\"","") domain = os.environ.get("GANDI_DOMAIN","").replace("\"","") -subdomains = os.environ.get("GANDI_SUBDOMAINS","").replace("\"","").split(",") +subdomains = os.environ.get("GANDI_SUBDOMAINS","").split(",") ttl = os.environ.get("GANDI_TIMEOUT","300").replace("\"","") -ifconfig = os.environ.get("GANDI_IFCONFIG","https://ifconfig.co/ip").replace("\"","") +ifconfig = os.environ.get("GANDI_IFCONFIG","https://icanhazip.com/").replace("\"","") diff --git a/src/gandi-live-dns.py b/src/gandi-live-dns.py index 3f40018..0eb197e 100755 --- a/src/gandi-live-dns.py +++ b/src/gandi-live-dns.py @@ -95,14 +95,16 @@ def update_records(uuid, dynIP, subdomain): def main(force_update, verbosity): if verbosity: print("verbosity turned on - not implemented by now") - + if config.api_secret == "" or config.domain == "" or config.subdomains == "" or len(config.subdomains) == 0: + print("invalid config") + exit() #get zone ID from Account uuid = get_uuid() #compare dynIP and DNS IP dynIP = get_dynip(config.ifconfig) dnsIP = get_dnsip(uuid) - + if force_update: print("Going to update/create the DNS Records for the subdomains") for sub in config.subdomains: diff --git a/src/gandi-live-dns.py.bak b/src/gandi-live-dns.py.bak new file mode 100755 index 0000000..55e5757 --- /dev/null +++ b/src/gandi-live-dns.py.bak @@ -0,0 +1,130 @@ +#!/usr/bin/env python +# encoding: utf-8 +''' +Gandi v5 LiveDNS - DynDNS Update via REST API and CURL/requests + +@author: cave +License GPLv3 +https://www.gnu.org/licenses/gpl-3.0.html + +Created on 13 Aug 2017 +http://doc.livedns.gandi.net/ +http://doc.livedns.gandi.net/#api-endpoint -> https://dns.gandi.net/api/v5/ +''' + +import requests, json +import config +import argparse + + +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 + ''' + r = requests.get(ifconfig_provider) + print 'Checking dynamic IP: ' , r._content.strip('\n') + return r.content.strip('\n') + +def get_uuid(): + ''' + find out ZONE UUID from domain + Info on domain "DOMAIN" + GET /domains/: + + ''' + url = config.api_endpoint + '/domains/' + config.domain + 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'] + exit() + +def get_dnsip(uuid): + ''' find out IP from first Subdomain DNS-Record + List all records with name "NAME" and type "TYPE" in the zone UUID + GET /zones//records//: + + The first subdomain from config.subdomain will be used to get + the actual DNS Record IP + ''' + + url = config.api_endpoint+ '/zones/' + uuid + '/records/' + config.subdomains[0] + '/A' + headers = {"X-Api-Key":config.api_secret} + 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') + else: + 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): + ''' update DNS Records for Subdomains + Change the "NAME"/"TYPE" record from the zone UUID + PUT /zones//records//: + curl -X PUT -H "Content-Type: application/json" \ + -H 'X-Api-Key: XXX' \ + -d '{"rrset_ttl": 10800, + "rrset_values": [""]}' \ + https://dns.gandi.net/api/v5/zones//records// + ''' + 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} + 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 + return True + else: + 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): + + if verbosity: + print "verbosity turned on - not implemented by now" + + + #get zone ID from Account + uuid = get_uuid() + + #compare dynIP and DNS IP + dynIP = get_dynip(config.ifconfig) + dnsIP = get_dnsip(uuid) + + if force_update: + 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" + else: + 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) + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument('-v', '--verbose', help="increase output verbosity", action="store_true") + parser.add_argument('-f', '--force', help="force an update/create", action="store_true") + args = parser.parse_args() + + + main(args.force, args.verbose) + + + + + +