Change the function to get the IPv4 active address to use in the autodiscover mode (issue #452)

pull/454/head
Nicolargo 2014-12-03 22:51:14 +01:00
parent b743d6ace0
commit 03743421f0
1 changed files with 14 additions and 4 deletions

View File

@ -177,11 +177,11 @@ class GlancesAutoDiscoverClient(object):
def __init__(self, hostname, args=None): def __init__(self, hostname, args=None):
if netifaces_tag: if netifaces_tag:
try: # -B @ overwrite the dynamic IPv4 choice
zeroconf_bind_address = netifaces.ifaddresses( if args.bind_address != '0.0.0.0':
netifaces.interfaces()[1])[netifaces.AF_INET][0]['addr']
except:
zeroconf_bind_address = args.bind_address zeroconf_bind_address = args.bind_address
else:
zeroconf_bind_address = self.find_active_ip_address()
print("Announce the Glances server on the local area network (using %s IP address)" % print("Announce the Glances server on the local area network (using %s IP address)" %
zeroconf_bind_address) zeroconf_bind_address)
@ -211,6 +211,16 @@ class GlancesAutoDiscoverClient(object):
logger.error( logger.error(
"Can not announce Glances server on the network (Netifaces lib is not installed)") "Can not announce Glances server on the network (Netifaces lib is not installed)")
def find_active_ip_address(self):
"""Try to find the active IP addresses"""
try:
# Interface of the default gatewau
gateway_itf = netifaces.gateways()['default'][netifaces.AF_INET][1]
# IP address for the interface
return netifaces.ifaddresses(gateway_itf)[netifaces.AF_INET][0]['addr']
except:
return None
def close(self): def close(self):
if zeroconf_tag: if zeroconf_tag:
self.zeroconf.unregister_service(self.info) self.zeroconf.unregister_service(self.info)