Retrieving The Network Configuration of A Local Machine
Retrieving The Network Configuration of A Local Machine
In every LAN, a host is configured to act as a gateway, which talks to the outside
world. To find the network address and the netmask, we can use a Python third-
party library, netifaces. For example, you can call netifaces.gateways() to find the
gateways that are configured to the outside world. Similarly, you can enumerate
the network interfaces by calling netifaces.interfaces(). If you would like to know
all the IP addresses of a particular interface, eth0, then you can call
netifaces.ifaddresses('eth0').
The following code listing shows the way in which you can list all the gateways
and IP addresses of a local machine.
import socket
import netifaces
If you run this code in a Windows operating system, it will print a summary of
the local network configuration, which will be similar to the following: