I want to detect the available network interfaces that is connected to a DHCP and got an IP from it. I am using following script to generate the list of available adapters.
import psutil
addrs = psutil.net_if_addrs()
all_network_interfaces = addrs.keys()
available_networks = []
for value in all_network_interfaces:
if addrs[value][1][1].startswith("169.254"):
continue
else:
available_networks.append(value)
print(available_networks)
The ones that starts with 169.254 are the adapters that is using Automatic Private IP Addressing (APIPA) so I want to filter them out. When I connect with an Ethernet cable, this script shows the related adapter, if I also connect over WiFi while ethernet is still connected, it adds the WiFi to the list. However, after disconnecting from the WiFi, WiFi adapters still holds the IP and still exists in the list. I think that it is a problem( maybe feature) with my adapter card. What is the best way to bypass this and obtain only the adapters with DHCP connection?