Python – Network Interface

When we have multiple interfaces in a machine we need to keep track of their names, status etc. In Python we can list the interfaces and their status.


In the below example we use the python module netifaces which gives the details of the interfaces and their status. The methods used are very simple and straight forward.

import netifaces

print (netifaces.interfaces())

print (netifaces.ifaddresses('lo'))

print (netifaces.AF_LINK)

addrs = netifaces.ifaddresses('ens33')


When we run the above program, we get the following output −

># Result

['lo', 'ens33']
{17: [{'peer': '00:00:00:00:00:00', 'addr': '00:00:00:00:00:00'}], 2: [{'peer': '', 'addr': '', 'netmask': ''}], 10: [{'addr': '::1', 'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128'}]}


[{'netmask': '', 'addr': '', 'broadcast': ''}]
[{'addr': '00:0c:29:ea:13:0a', 'broadcast': 'ff:ff:ff:ff:ff:ff'}]