Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions lib/ddupdate/ddplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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<link>' 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<link>' 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)
Expand Down
21 changes: 16 additions & 5 deletions plugins/addr_default_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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'
Expand All @@ -37,15 +38,25 @@ 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)
break
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
3 changes: 2 additions & 1 deletion plugins/addr_hardcoded_if.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class HardcodedIfPlugin(AddressPlugin):

Options:
if=interface
link
"""

_name = 'hardcoded-if'
Expand All @@ -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