Initial commit

This commit is contained in:
ayakael 2020-06-01 20:26:21 -04:00 committed by Antoine Martin
commit d530899dfc
Signed by: forge
GPG key ID: D62A472A4AA7D541
3 changed files with 116 additions and 0 deletions

72
zoneupdate.py Normal file
View file

@ -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')

23
zoneupdate.sh Normal file
View file

@ -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

21
zoneupdate.toml Normal file
View file

@ -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"
]