Commit 5fbd37cd authored by Johannes Winter's avatar Johannes Winter

dyndns updater

parent 3a3f0b5c
#!/usr/bin/env python
## imports
import json
import requests
import os, sys
## loaders
from dotenv import load_dotenv
env_path = os.path.abspath ( os.path.join ( os.curdir, os.path.relpath('.env') ) )
load_dotenv(verbose=True, dotenv_path=env_path)
## constants
PDNS_API_BASE = os.getenv("PDNS_API_BASE")
PDNS_TOKEN = os.getenv("PDNS_TOKEN")
def pprint(text):
print (json.dumps(text, sort_keys=True, indent=4, separators=(',', ': ')))
def get_ip():
r = requests.get('http://ipinfo.io')
return (r.json()['ip'])
def set_record(zone, name, type, record):
payload = {
'rrsets': [
{
'name': name,
'type': type,
'ttl': 1,
'changetype': "REPLACE",
'records': [ record ]
}
]
}
print ('payload')
pprint(payload)
p = requests.patch(PDNS_API_BASE + 'zones/' + zone, headers={'X-API-KEY': PDNS_TOKEN}, json=payload)
print (p.text)
print (p.status_code)
record = {
'disabled': False,
'content': get_ip()
}
if len(sys.argv) > 1:
if len(sys.argv) > 2:
record = {
'disabled': False,
'content': get_ip()
}
set_record (sys.argv[1], sys.argv[2]+'.', 'A', record)
else:
print('Updates ip record of NAME in ZONE to current IP from ipinfo.io\n\nUsage:\n' + sys.argv[0] + ' ZONE NAME')
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment