Refactor port to take into account netifaces2

This commit is contained in:
nicolargo 2025-10-23 11:43:22 +02:00
parent c2e038d244
commit 1ac760c55b
1 changed files with 5 additions and 13 deletions

View File

@ -13,19 +13,10 @@ from glances.logger import logger
try:
import netifaces
netifaces.default_gateway()
netifaces_tag = True
except ImportError:
logger.warning("Ports plugin - Netifaces2 lib not found, port_default_gateway feature is disabled")
netifaces_tag = False
try:
netifaces.gateways()
except Exception:
netifaces_tag = True
else:
logger.warning(
"Ports plugin - Netifaces2 do not support gateways() method, port_default_gateway feature is disabled"
)
except (ImportError, NameError):
logger.warning("Ports plugin - Can not init Netifaces2 lib, port_default_gateway feature is disabled")
netifaces_tag = False
@ -61,7 +52,7 @@ class GlancesPortsList:
if default_gateway.lower().startswith('true') and netifaces_tag:
new_port = {}
try:
new_port['host'] = netifaces.gateways()[netifaces.AF_INET][0][0]
new_port['host'] = netifaces.default_gateway()[netifaces.AF_INET][0]
except (KeyError, NameError):
new_port['host'] = None
# ICMP
@ -127,3 +118,4 @@ class GlancesPortsList:
def set_server(self, pos, key, value):
"""Set the key to the value for the pos (position in the list)."""
self._ports_list[pos][key] = value
self._ports_list[pos][key] = value