Config file now uses yaml format

This commit is contained in:
Antoine Martin 2023-05-14 17:09:54 -04:00
parent ae9b885fc4
commit f699af1eef
Signed by: forge
GPG key ID: D62A472A4AA7D541
3 changed files with 31 additions and 32 deletions

View file

@ -1,11 +1,13 @@
#!/bin/python3 #! /usr/bin/env python3
import requests import requests
import json import json
import toml import yaml
import re 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): 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) json_object = json.loads(resp._content)
return json_object return json_object
for domain in config['domains']: for zone in config['zones']:
for subdomain in config[domain]['subdomains']: domain = zone['domain']
for subdomain in zone['subdomains']:
url = subdomain + '.' + domain url = subdomain + '.' + domain
api_url=config['api_url'] api_url=config['api_url']
api_key=config[domain]['api_key'] api_key=zone['api_key']
debug=config['debug'] debug=config['debug']
ttl=config['ttl'] ttl=config['ttl']
ip_services=config['ip_services']
print ("Checking IP of %s" % url) print ("Checking IP of %s" % url)
try: 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=(sub_records['rrset_values'])
old_ip=old_ip[0] 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: except Exception as e:
print ('Failed to find sub-record for domain %s, attempting to create' % url) 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']) new_ip=get_current_public_ip(ip_services=ip_services, debug=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']) resp = update_sub_records(domain,subdomain,api_url=api_url,api_key=api_key,ip=new_ip,ttl=ttl,debug=debug)
print (resp) print (resp)
continue continue
if new_ip != old_ip: if new_ip != old_ip:
print ('IP check failed, updating') 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) print (resp)
else: else:
print ('IP check complete') print ('IP check complete')

View file

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

16
zoneupdate.yml Normal file
View file

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