Match different server successful response, Fixed error log, Added date and time on logs

This commit is contained in:
Daniele Verducci (Slimpenguin) 2024-01-11 07:59:01 +01:00
parent 07e1126a58
commit 998315ef05

View File

@ -48,13 +48,13 @@ import json
NAME = 'mddclient' NAME = 'mddclient'
VERSION = '0.1' VERSION = '0.2'
DESCRIPTION = 'A DynamicDns client like ddclient, but supporting multiple (sub)domains' DESCRIPTION = 'A DynamicDns client like ddclient, but supporting multiple (sub)domains'
STATUS_FILE = '/tmp/mddclient.tmp' STATUS_FILE = '/tmp/mddclient.tmp'
CHECKIP_REQUEST_ADDR = 'http://checkip.dyndns.org' CHECKIP_REQUEST_ADDR = 'http://checkip.dyndns.org'
CHECKIP_RESPONSE_PARSER = '<body>Current IP Address: (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})</body>' CHECKIP_RESPONSE_PARSER = '<body>Current IP Address: (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})</body>'
DDCLIENT2_REQUEST_ADDR = "https://{}/nic/update?system=dyndns&hostname={}&myip={}" DDCLIENT2_REQUEST_ADDR = "https://{}/nic/update?system=dyndns&hostname={}&myip={}"
DDCLIENT2_RESPONSE_PARSER = '^(nochg|good) (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})$' DDCLIENT2_RESPONSE_PARSER = '^(nochg|no_change|good) (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3})$'
USER_AGENT = 'Selfhost Utils Mddclient ' + VERSION USER_AGENT = 'Selfhost Utils Mddclient ' + VERSION
class Main: class Main:
@ -121,11 +121,7 @@ class Main:
def getCurrentIp(self): def getCurrentIp(self):
'''Obtains current IP from checkip.dyndns.org''' '''Obtains current IP from checkip.dyndns.org'''
try: response = requests.get(CHECKIP_REQUEST_ADDR)
response = requests.get(CHECKIP_REQUEST_ADDR)
except Exception as e:
self._log.error('Unable to obtain new IP addr: connection error: {}'.format(e))
return
match = re.search(CHECKIP_RESPONSE_PARSER, response.text, re.MULTILINE) match = re.search(CHECKIP_RESPONSE_PARSER, response.text, re.MULTILINE)
if not match: if not match:
@ -159,7 +155,7 @@ class Main:
if operationResult == 'good': if operationResult == 'good':
# Success! # Success!
return ipAddr return ipAddr
elif operationResult == 'nochg': elif operationResult == 'nochg' or operationResult == 'no_change':
# Should not happen: IP didn't need update # Should not happen: IP didn't need update
self._log.warning('Ip addres didn\'t need update: this should happen only at first run') self._log.warning('Ip addres didn\'t need update: this should happen only at first run')
return ipAddr return ipAddr
@ -182,7 +178,7 @@ class Main:
elif operationResult == '911': elif operationResult == '911':
raise Exception('There is a problem or scheduled maintenance on server side') raise Exception('There is a problem or scheduled maintenance on server side')
else: else:
raise Exception('Server returned an unknown result code: {}}'.format(operationResult)) raise Exception('Server returned an unknown result code: {}'.format(operationResult))
class Status: class Status:
@ -268,7 +264,11 @@ if __name__ == '__main__':
level = logging.WARNING level = logging.WARNING
else: else:
level = logging.INFO level = logging.INFO
logging.basicConfig(level=level, format='%(asctime)s %(message)s') logging.basicConfig(
format='%(asctime)s %(levelname)-8s %(message)s',
level=level,
datefmt='%Y-%m-%d %H:%M:%S'
)
try: try:
main = Main(args.configFile) main = Main(args.configFile)