Change default interface algorithm on linux to match that used on other platforms

i.e. use first address of the interface that has the default gateway
This commit is contained in:
Kovid Goyal 2016-01-09 21:24:04 +05:30
parent 517f25920c
commit 50b21e0d70

View File

@ -57,12 +57,14 @@ elif isosx:
return addr return addr
else: else:
def get_default_route_src_address(to='1.1.1.1'): def get_default_route_src_address():
# Use -6 for IPv6 addresses # Use /proc/net/ipv6_route for IPv6 addresses
raw = subprocess.check_output(('ip -4 route get ' + to).split()).decode('utf-8') raw = open('/proc/net/route', 'rb').read().decode('utf-8')
m = re.search('^%s\s+via\s+%s\s+.+?src\s+(.+)' % (to, get_address_of_default_gateway()), raw, flags=re.MULTILINE) for line in raw.splitlines():
if m is not None: parts = line.split()
return m.group(1) if parts[1] == '00000000':
for addr in get_addresses_for_interface(parts[0]):
return addr
if __name__ == '__main__': if __name__ == '__main__':
print(get_default_route_src_address()) print(get_default_route_src_address())