From d530899dfca3542c0f3dfce426c4a7b339293b49 Mon Sep 17 00:00:00 2001 From: ayakael Date: Mon, 1 Jun 2020 20:26:21 -0400 Subject: [PATCH] Initial commit --- zoneupdate.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ zoneupdate.sh | 23 ++++++++++++++++ zoneupdate.toml | 21 +++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 zoneupdate.py create mode 100644 zoneupdate.sh create mode 100644 zoneupdate.toml diff --git a/zoneupdate.py b/zoneupdate.py new file mode 100644 index 0000000..1c320c8 --- /dev/null +++ b/zoneupdate.py @@ -0,0 +1,72 @@ +#!/bin/python3 + +import requests +import json +import toml +import re + +config=toml.load('zoneupdate.toml') + +def get_current_public_ip(ip_services, debug=False): + ''' + resolve public ip using simple service, from github.com/fyah/gandi-api-v5/gandyn-livedns.py + ''' + for service in ip_services: + try: + r = requests.get(service) + res = r.text + if re.match('\d+\.\d+\.\d+\.\d+', res): + return res + except Exception as e: + #there was a problem resolving our public with the service... + if debug: + print ('Failed to resolve public ip using %s' % service) + print (e) + #try with next + continue + + +def get_sub_records(domain, subdomain, api_url, api_key, debug=False): + url = api_url + '/domains/' + domain + '/records/' + subdomain + headers = {'Authorization': "Apikey " + api_key} + resp = requests.get(url, headers=headers) + json_object = json.loads(resp._content) + return json_object[0] + +def update_sub_records(domain, subdomain, api_url, api_key, ip, ttl, debug=False): + url = api_url + '/domains/' + domain + '/records/' + subdomain + payload = {"rrset_type": "A", "rrset_ttl": ttl, "rrset_values": [ip]} + items = {"items": [payload]} + headers = {'Content-Type': 'application/json', 'Authorization': "Apikey " + api_key} + resp = requests.put(url, data=json.dumps(items), headers=headers) + json_object = json.loads(resp._content) + return json_object + +for domain in config['domains']: + for subdomain in config[domain]['subdomains']: + url = subdomain + '.' + domain + api_url=config['api_url'] + api_key=config[domain]['api_key'] + debug=config['debug'] + ttl=config['ttl'] + + print ("Checking IP of %s" % url) + try: + sub_records = get_sub_records(domain,subdomain,api_url=config['api_url'],api_key=config[domain]['api_key'],debug=config['debug']) + old_ip=(sub_records['rrset_values']) + old_ip=old_ip[0] + new_ip=get_current_public_ip(ip_services=config['ip_services'], debug=config['debug']) + + except Exception as e: + print ('Failed to find sub-record for domain %s, attempting to create' % url) + new_ip=get_current_public_ip(ip_services=config['ip_services'], debug=config['debug']) + resp = update_sub_records(domain,subdomain,api_url=config['api_url'],api_key=config[domain]['api_key'],ip=new_ip,ttl=config['ttl'],debug=config['debug']) + print (resp) + + continue + if new_ip != old_ip: + print ('IP check failed, updating') + resp = update_sub_records(domain,subdomain,api_url=config['api_url'],api_key=config[domain]['api_key'],ip=new_ip,ttl=config['ttl'],debug=config['debug']) + print (resp) + else: + print ('IP check complete') diff --git a/zoneupdate.sh b/zoneupdate.sh new file mode 100644 index 0000000..716da85 --- /dev/null +++ b/zoneupdate.sh @@ -0,0 +1,23 @@ +#!/bin/bash +APIKEY="$(awk 'BEGIN {RS="\n"; FS="\t";}{if($1=="APIKEY"){print $2}}' /etc/zoneupdate.conf)" +DOMAIN="$(awk 'BEGIN {RS="\n"; FS="\t";}{if($1=="DOMAIN"){print $2}}' /etc/zoneupdate.conf)" +subdomainList=(@ "$(awk 'BEGIN {RS="\n"; FS="\t";}{if($1=="SUBDOMAIN"){print $2}}' /etc/zoneupdate.conf)") +OLD_IP="$(awk 'BEGIN {RS="\n"; FS="\t";}{if($1=="IP"){print $2}}' /etc/zoneupdate.conf)" +NEW_IP=$(curl -4 -s icanhazip.com) + + +if [[ "${OLD_IP}" != "${NEW_IP}" ]]; then + for subdomain in ${subdomainList[@]}; do + RESP=$(curl -s -X PUT -H "Content-Type: application/json" \ + -H "X-Api-Key: $APIKEY" \ + -d '{"items": [{"rrset_type": "A", + "rrset_ttl": 3600, + "rrset_values":["'$NEW_IP'"]}]}' \ + https://dns.api.gandi.net/api/v5/domains/$DOMAIN/records/$subdomain) + [[ "${RESP}" != '{"message": "DNS Record Created"}' ]] && ERROR=true + done + [[ "${ERROR}" != "true" ]] && { gawk -i inplace -v IP="${NEW_IP}" 'BEGIN {RS="\n"; FS="\t"; OFS="\t"}{if($1=="IP"){$2=IP}{print $0}}' /etc/zoneupdate.conf; exit 0; } || { echo ${RESP}; exit 1; } +else + exit 0 +fi + diff --git a/zoneupdate.toml b/zoneupdate.toml new file mode 100644 index 0000000..dd478b7 --- /dev/null +++ b/zoneupdate.toml @@ -0,0 +1,21 @@ +debug = "true" +api_url = "https://api.gandi.net/v5/livedns" +ttl = 3600 +ip_services = [ + 'https://ifconfig.co', + 'http://ifconfig.me/ip', + 'http://whatismyip.akamai.com/', + 'http://ipinfo.io/ip', + 'http://icanhazip.org' +] +domains = [ + "example.com" +] + +["example.com"] + api_key = "gandi-net-api-key" + subdomains = [ + "@", + "www", + "test" + ]