mirror of
git://github.com/kovidgoyal/calibre.git
synced 2026-05-08 19:13:40 +02:00
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:
parent
517f25920c
commit
50b21e0d70
1 changed files with 8 additions and 6 deletions
|
|
@ -57,12 +57,14 @@ def get_default_route_src_address():
|
|||
return addr
|
||||
else:
|
||||
|
||||
def get_default_route_src_address(to='1.1.1.1'):
|
||||
# Use -6 for IPv6 addresses
|
||||
raw = subprocess.check_output(('ip -4 route get ' + to).split()).decode('utf-8')
|
||||
m = re.search('^%s\s+via\s+%s\s+.+?src\s+(.+)' % (to, get_address_of_default_gateway()), raw, flags=re.MULTILINE)
|
||||
if m is not None:
|
||||
return m.group(1)
|
||||
def get_default_route_src_address():
|
||||
# Use /proc/net/ipv6_route for IPv6 addresses
|
||||
raw = open('/proc/net/route', 'rb').read().decode('utf-8')
|
||||
for line in raw.splitlines():
|
||||
parts = line.split()
|
||||
if parts[1] == '00000000':
|
||||
for addr in get_addresses_for_interface(parts[0]):
|
||||
return addr
|
||||
|
||||
if __name__ == '__main__':
|
||||
print(get_default_route_src_address())
|
||||
|
|
|
|||
Loading…
Reference in a new issue