How to get Network Interface Card names in Python?

后端 未结 9 1832
灰色年华
灰色年华 2020-12-06 09:36

I am totally new to python programming so please be patient with me.

Is there anyway to get the names of the NIC cards in the machine etc. eth0, lo? If so how do you

9条回答
  •  难免孤独
    2020-12-06 09:58

    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())
    

提交回复
热议问题