IP plugin displays wrong interface: outer loop in get_ip_address() never breaks
#3,617 opened on Jul 21, 2026
Repository metrics
- Stars
- (33,243 stars)
- PR merge metrics
- (Avg merge 14h 28m) (2 merged PRs in 30d)
Description
The get_ip_address() function in glances/globals.py is supposed to return the IP address of the first active (up, non-loopback) network interface, but due to a missing break in the outer loop, it actually returns the address of the last qualifying interface instead.
Code in question (glances/globals.py, get_ip_address()):
python def get_ip_address(ipv6=False): """Get current IP address and netmask as a tuple.""" family = socket.AF_INET6 if ipv6 else socket.AF_INET stats = psutil.net_if_stats() addrs = psutil.net_if_addrs() ip_address = None ip_netmask = None for interface, stat in stats.items(): if stat.isup and interface != 'lo': if interface in addrs: for addr in addrs[interface]: if addr.family == family: ip_address = addr.address ip_netmask = addr.netmask break # only breaks the inner loop return ip_address, ip_netmask
The inner break only exits the loop over addresses for a single interface — the outer loop over stats.items() keeps going and overwrites ip_address/ip_netmask for every subsequent up, non-loopback interface that has a matching address family. So the function silently returns whichever qualifying interface happens to be last in psutil.net_if_stats()'s dict order, not the first.
Impact
On any host with Docker (or other virtual bridge/veth interfaces) installed, this reliably causes the [ip] plugin to display the Docker bridge's internal IP (e.g. 172.18.0.1) instead of the real host IP (e.g. 192.168.0.150) on the dashboard, because the bridge/veth interfaces are isup=True and appear later in net_if_stats() than the physical NIC.
Steps to reproduce
Run Glances on a host with Docker installed and at least one running container (so a br-xxxx bridge interface exists and is up). Open the Web UI or TUI dashboard. Observe the IP field shows the Docker bridge address rather than the host's real LAN address.
Environment
Glances version: 4.5.5 PsUtil version: 7.2.2 OS: Ubuntu (systemd service, standard install, not containerized)
Example psutil.net_if_stats() order on affected host:
['lo', 'enp1s0', 'docker0', 'br-cd45b0d15fb9', 'vethd553473', ...]
enp1s0 is second (correct answer), but br-cd45b0d15fb9 (up, with an IPv4 address) comes later and overwrites it.
Suggested fix
Add a second break (or an early return) once a valid address is found, so the outer loop stops at the first qualifying interface:
python if stat.isup and interface != 'lo': if interface in addrs: for addr in addrs[interface]: if addr.family == family: ip_address = addr.address ip_netmask = addr.netmask break if ip_address: break
I've tested this patch locally and it correctly resolves the private IP to the physical host interface instead of the Docker bridge.