diff --git a/lib/ddupdate/ddplugin.py b/lib/ddupdate/ddplugin.py index 5947c3b..b8497ff 100644 --- a/lib/ddupdate/ddplugin.py +++ b/lib/ddupdate/ddplugin.py @@ -171,7 +171,7 @@ def empty(self): """Check if any address is set.""" return self.v4 is None and self.v6 is None - def parse_ifconfig_output(self, text): + def parse_ifconfig_output(self, text, link='false'): """ Update v4 and v6 attributes by parsing ifconfig(8) or ip(8) output. @@ -192,12 +192,16 @@ def parse_ifconfig_output(self, text): continue addr = words[1].split('/')[0] words = set(words[2:]) - if ('link' in words) or ('0x20' in words): - # don't use a link-local address - continue if 'deprecated' in words: # don't use a "deprecated" address continue + if ('link' in words) or ('0x20' in words): + if link.lower() == 'false': + # don't use a link-local address + continue + elif link.lower() == 'force': + # force use of a link-local address + continue self.v6 = addr if self.empty(): raise AddressError("Cannot find address for %s, giving up" % text) diff --git a/plugins/addr_default_ip.py b/plugins/addr_default_ip.py index cda9a24..3c7f37a 100644 --- a/plugins/addr_default_ip.py +++ b/plugins/addr_default_ip.py @@ -6,7 +6,7 @@ import subprocess -from ddupdate.ddplugin import AddressPlugin, AddressError, IpAddr +from ddupdate.ddplugin import AddressPlugin, AddressError, IpAddr, dict_of_opts def find_device(words): @@ -27,7 +27,8 @@ class DefaultIfPLugin(AddressPlugin): Digs in the routing tables and returns it's address using linux-specific code based on the ip utility which must be in $PATH - Options used: none + Options used: + link """ _name = 'default-if' @@ -37,8 +38,18 @@ def get_ip(self, log, options): """ Get default interface using ip route and address using ifconfig. """ + opts = dict_of_opts(options) if_ = None - for line in subprocess.getoutput('ip route').split('\n'): + remote = opts.get('remote', None) + if remote: + key = opts.get('key', None) + if key: + prefix = f"ssh -i {key} {remote} " + else: + prefix = f"ssh {remote} " + else: + prefix = "" + for line in subprocess.getoutput(''.join((prefix, 'ip route'))).split('\n'): words = line.split() if words[0] == 'default': if_ = find_device(words) @@ -46,6 +57,6 @@ def get_ip(self, log, options): if if_ is None: raise AddressError("Cannot find default interface, giving up") address = IpAddr() - output = subprocess.getoutput('ip address show dev ' + if_) - address.parse_ifconfig_output(output) + output = subprocess.getoutput(''.join((prefix, 'ip address show dev ', if_))) + address.parse_ifconfig_output(output, opts.get('link', 'false')) return address diff --git a/plugins/addr_hardcoded_if.py b/plugins/addr_hardcoded_if.py index b2d29e3..2ff714b 100644 --- a/plugins/addr_hardcoded_if.py +++ b/plugins/addr_hardcoded_if.py @@ -15,6 +15,7 @@ class HardcodedIfPlugin(AddressPlugin): Options: if=interface + link """ _name = 'hardcoded-if' @@ -28,5 +29,5 @@ def get_ip(self, log, options): if_ = opts['if'] address = IpAddr() output = subprocess.getoutput('ip address show dev ' + if_) - address.parse_ifconfig_output(output) + address.parse_ifconfig_output(output, opts.get('link', '0')) return address