diff --git a/zoneupdate.py b/zoneupdate.py index 1c320c8..38c4566 100644 --- a/zoneupdate.py +++ b/zoneupdate.py @@ -1,11 +1,13 @@ -#!/bin/python3 +#! /usr/bin/env python3 import requests import json -import toml +import yaml import re -config=toml.load('zoneupdate.toml') +from yaml.loader import SafeLoader +with open ('/etc/zoneupdate.conf', 'r') as file: + config = yaml.safe_load(file) def get_current_public_ip(ip_services, debug=False): ''' @@ -42,31 +44,33 @@ def update_sub_records(domain, subdomain, api_url, api_key, ip, ttl, debug=False json_object = json.loads(resp._content) return json_object -for domain in config['domains']: - for subdomain in config[domain]['subdomains']: +for zone in config['zones']: + domain = zone['domain'] + for subdomain in zone['subdomains']: url = subdomain + '.' + domain api_url=config['api_url'] - api_key=config[domain]['api_key'] + api_key=zone['api_key'] debug=config['debug'] ttl=config['ttl'] + ip_services=config['ip_services'] 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']) + sub_records = get_sub_records(domain,subdomain,api_url=api_url,api_key=api_key,debug=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']) + new_ip=get_current_public_ip(ip_services=ip_services, debug=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']) + 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) 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']) + resp = update_sub_records(domain,subdomain,api_url=api_url,api_key=api_key,ip=new_ip,ttl=ttl,debug=debug) print (resp) else: print ('IP check complete') diff --git a/zoneupdate.toml b/zoneupdate.toml deleted file mode 100644 index dd478b7..0000000 --- a/zoneupdate.toml +++ /dev/null @@ -1,21 +0,0 @@ -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" - ] diff --git a/zoneupdate.yml b/zoneupdate.yml new file mode 100644 index 0000000..7dadabe --- /dev/null +++ b/zoneupdate.yml @@ -0,0 +1,16 @@ +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' +zones: + - domain: example.com + api_key: "gandi-net-api-key" + subdomains: + - @ + - www + - test