From: Victor Wagner Date: Wed, 11 Sep 2019 20:10:14 +0000 (+0300) Subject: Fixed obvois errors. It even had done couple of update calls X-Git-Url: http://wagner.pp.ru/gitweb/?a=commitdiff_plain;h=f7cdd6fa207cf36d4b05bf69cb829a51a1225f3f;p=oss%2Fdyngo.git Fixed obvois errors. It even had done couple of update calls --- diff --git a/dyngo b/dyngo old mode 100644 new mode 100755 index ab21fde..dcfe779 --- a/dyngo +++ b/dyngo @@ -2,6 +2,7 @@ import ipaddress, subprocess, sys, getopt,dbm from configparser import ConfigParser import urllib.request,urllib.parse +import logging,os.path,time class DyndnsNetwork(object): """ This class represents dynamic DNS server and network. @@ -11,7 +12,7 @@ class DyndnsNetwork(object): self.hostname = config_section['hostname'] self.network = ipaddress.ip_network(config_section['network']) self.server = config_section['server'] - if user in config_section: + if 'user' in config_section: self.user=config_section["user"] self.password=config_section["password"] @@ -28,7 +29,8 @@ class DyndnsNetwork(object): Raises HTTPError if something goes wrong """ #construct opener - if hasattr(self,user): + logging.debug("Going to send message to %s",self.server) + if hasattr(self,'user'): password_mgr =urllib.request.HTTPPasswordMgrWithDefaultRealm() password_mgr.add_password(None,self.server,self.user,self.password) handler=urllib.request.HTTPBasicAuthHandler(password_mgr) @@ -44,6 +46,7 @@ class DyndnsNetwork(object): def get_current_addresses(): result=[] + logging.debug("Acquire current IPs") for line in subprocess.run(['ip','-o','addr','show'],check=True,stdout=subprocess.PIPE).stdout.decode('utf-8').split("\n"): if not len(line): continue @@ -52,6 +55,7 @@ def get_current_addresses(): if address.is_loopback or address.is_link_local: continue result.append(address) + logging.debug("Got following addresses: %s",repr(result)) return result def check_for_update(): addrlist=get_current_addresses() @@ -64,30 +68,32 @@ def check_for_update(): old_addr=ipaddress.ip_address(database[name].decode("utf-8")) if old_addr==a: # Nothing changed go, to next net + logging.debug("Address for net %s not changed",name) break # address changed try: + logging.info("Doing update for net %s with address %s",name, str(a)) net.nsupdate(a) database[name]=str(a) except urllib.error.HTTPError as e: - pass + logging.exception("Http error doing nsupdate code %s",e.code) break if not found: + logging.info("Address for net %s no more found",name) del data[name] - - - config=ConfigParser() -config['dyngo']={'interval':'60','database':'/var/lib/dyngo/dyngo.db','ca':'/etc/ssl/certs'} +config['dyngo']={'interval':'60','database':'/var/lib/dyngo/dyngo.db', + 'ca':'/etc/ssl/certs','loglevel':'WARNING'} options=dict(getopt.getopt(sys.argv,"f:")[0]) if not '-f' in options: options["-f"]="/etc/dyngo.conf" if len(config.read(options["-f"]))!=1: print("Cannot read config %s"%options["-f"],file=sys.stderr) sys.exit(1) - conf=config['dyngo'] +logging.basicConfig(format="%(asctime)s %(message)s", + level=getattr(logging,conf['loglevel'].upper())) interval=int(conf['interval']) database=dbm.open(conf['database'],"c") https_params={} @@ -102,13 +108,14 @@ networks={} for sect in config.sections(): if sect == 'dyngo' or sect == 'DEFAULT': continue + logging.debug("Processing network %s",sect) networks[sect]=DyndnsNetwork(config[sect]) # Remove stale items from persistent database, which are no more # mentioned in the config file -for i in set([x.decode("utf-8") for x in database.keys()])-set(network.keys()): +for i in set([x.decode("utf-8") for x in database.keys()])-set(networks.keys()): + logging.info("Removing from persistent state network %s which is no more in config",i) del database[i] - while True: check_for_update() time.sleep(interval)