Commit 5337a0ae authored by Johannes Winter's avatar Johannes Winter

log to syslog

parent 736a2905
......@@ -4,10 +4,20 @@
import json
import requests
import os, sys
## loaders
import logging
import logging.handlers
from dotenv import load_dotenv
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
handler = logging.handlers.SysLogHandler(address = '/dev/log')
formatter = logging.Formatter("dyndns-update: %(message)s")
handler.setFormatter(formatter)
my_logger.addHandler(handler)
env_path = os.path.abspath ( os.path.join ( os.curdir, os.path.relpath('.env') ) )
load_dotenv(verbose=True, dotenv_path=env_path)
......@@ -17,9 +27,6 @@ PDNS_API_BASE = os.getenv("PDNS_API_BASE")
PDNS_TOKEN = os.getenv("PDNS_TOKEN")
TMPFILE = '/tmp/lastIP'
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'])
......@@ -36,29 +43,24 @@ def set_record(zone, name, type, 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)
return (p.status_code)
if len(sys.argv) > 2:
if os.path.exists(TMPFILE):
with open(TMPFILE) as file:
try:
lastIP=file.read()
except:
printf('error reading ' + TMPFILE)
my_logger.critic('error reading ' + TMPFILE)
else:
lastIP = ''
currentIP = get_ip()
if lastIP == currentIP:
print ('IP has not changed since last call')
my_logger.debug ('IP has not changed since last call')
exit()
record = {
......@@ -66,7 +68,8 @@ if len(sys.argv) > 2:
'content': currentIP
}
set_record (sys.argv[1], sys.argv[2]+'.', 'A', record)
if (set_record (sys.argv[1], sys.argv[2]+'.', 'A', record) == 204):
my_logger.debug('IP of ' + sys.argv[2] + ' successfully changed to ' + currentIP)
with open(TMPFILE, 'w') as file:
file.write(currentIP)
......
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