Python Network Programming Network Interface

Python Network Programming: Network Interfaces

When we have multiple interfaces on a machine, we need to keep track of their names, status, and so on. In Python, we can list these interfaces and their status.

Example

In the following example, we use the Python module netifaces, which provides detailed information about interfaces and their status. The method is very simple and straightforward.

import netifaces

print (netifaces.interfaces())

print (netifaces.ifaddresses('lo'))

print (netifaces.AF_LINK)

addrs = netifaces.ifaddresses('ens33')
print(addrs[netifaces.AF_INET])

print(addrs[netifaces.AF_LINK])

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': '127.0.0.1', 'addr': '127.0.0.1', 'netmask': '255.0.0.0'}], 10: [{'addr': '::1', 'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128'}]}

17

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

Leave a Reply

Your email address will not be published. Required fields are marked *