提交 03743421 编写于 作者: N Nicolargo

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

上级 b743d6ac
......@@ -177,11 +177,11 @@ class GlancesAutoDiscoverClient(object):
def __init__(self, hostname, args=None):
if netifaces_tag:
try:
zeroconf_bind_address = netifaces.ifaddresses(
netifaces.interfaces()[1])[netifaces.AF_INET][0]['addr']
except:
# -B @ overwrite the dynamic IPv4 choice
if args.bind_address != '0.0.0.0':
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)" %
zeroconf_bind_address)
......@@ -211,6 +211,16 @@ class GlancesAutoDiscoverClient(object):
logger.error(
"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):
if zeroconf_tag:
self.zeroconf.unregister_service(self.info)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册