zoneupdate/zoneupdate.py

77 lines
2.8 KiB
Python
Raw Normal View History

2023-05-14 21:09:54 +00:00
#! /usr/bin/env python3
2020-06-02 00:26:21 +00:00
import requests
import json
2023-05-14 21:09:54 +00:00
import yaml
2020-06-02 00:26:21 +00:00
import re
2023-05-14 21:09:54 +00:00
from yaml.loader import SafeLoader
with open ('/etc/zoneupdate.conf', 'r') as file:
config = yaml.safe_load(file)
2020-06-02 00:26:21 +00:00
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
2023-05-14 21:09:54 +00:00
for zone in config['zones']:
domain = zone['domain']
for subdomain in zone['subdomains']:
2020-06-02 00:26:21 +00:00
url = subdomain + '.' + domain
api_url=config['api_url']
2023-05-14 21:09:54 +00:00
api_key=zone['api_key']
2020-06-02 00:26:21 +00:00
debug=config['debug']
ttl=config['ttl']
2023-05-14 21:09:54 +00:00
ip_services=config['ip_services']
2020-06-02 00:26:21 +00:00
print ("Checking IP of %s" % url)
try:
2023-05-14 21:09:54 +00:00
sub_records = get_sub_records(domain,subdomain,api_url=api_url,api_key=api_key,debug=debug)
2020-06-02 00:26:21 +00:00
old_ip=(sub_records['rrset_values'])
old_ip=old_ip[0]
2023-05-14 21:09:54 +00:00
new_ip=get_current_public_ip(ip_services=ip_services, debug=debug)
2020-06-02 00:26:21 +00:00
except Exception as e:
print ('Failed to find sub-record for domain %s, attempting to create' % url)
2023-05-14 21:09:54 +00:00
new_ip=get_current_public_ip(ip_services=ip_services, debug=debug)
resp = update_sub_records(domain,subdomain,api_url=api_url,api_key=api_key,ip=new_ip,ttl=ttl,debug=debug)
2020-06-02 00:26:21 +00:00
print (resp)
continue
if new_ip != old_ip:
print ('IP check failed, updating')
2023-05-14 21:09:54 +00:00
resp = update_sub_records(domain,subdomain,api_url=api_url,api_key=api_key,ip=new_ip,ttl=ttl,debug=debug)
2020-06-02 00:26:21 +00:00
print (resp)
else:
print ('IP check complete')