How to get Network Interface Card names in Python?

A great Python library I have used to do this is psutil. It can be used on Linux, Windows, and OSX among other platforms and is supported from Python 2.6 to 3.6.

Psutil provides the net_if_addrs() function which returns a dictionary where keys are the NIC names and value is a list of named tuples for each address assigned to the NIC which include the address family, NIC address, netmask, broadcast address, and destination address.

A simple example using net_if_addrs() which will print a Python list of the NIC names:

import psutil

addrs = psutil.net_if_addrs()
print(addrs.keys())

Leave a Comment