How to get Network Interface Card names in Python?

后端 未结 9 1772
灰色年华
灰色年华 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:48

    On Linux, you can just list the links in /sys/class/net/ by

    os.listdir('/sys/class/net/')
    

    Not sure if this works on all distributions.

    0 讨论(0)
  • 2020-12-06 09:50

    socket module in Python >= 3.3:

    import socket
    
    # Return a list of network interface information
    socket.if_nameindex()
    

    https://docs.python.org/3/library/socket.html#socket.if_nameindex

    0 讨论(0)
  • 2020-12-06 09:51

    Using python's ctypes you can make a call to the C library function getifaddrs:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from ctypes import *
    
    class Sockaddr(Structure):
        _fields_ = [('sa_family', c_ushort), ('sa_data', c_char * 14)]
    
    class Ifa_Ifu(Union):
        _fields_ = [('ifu_broadaddr', POINTER(Sockaddr)),
                    ('ifu_dstaddr', POINTER(Sockaddr))]
    
    class Ifaddrs(Structure):
        pass
    
    Ifaddrs._fields_ = [('ifa_next', POINTER(Ifaddrs)), ('ifa_name', c_char_p),
                        ('ifa_flags', c_uint), ('ifa_addr', POINTER(Sockaddr)),
                        ('ifa_netmask', POINTER(Sockaddr)), ('ifa_ifu', Ifa_Ifu),
                        ('ifa_data', c_void_p)]
    
    
    def get_interfaces():
        libc = CDLL('libc.so.6')
        libc.getifaddrs.restype = c_int
        ifaddr_p = pointer(Ifaddrs())
        ret = libc.getifaddrs(pointer((ifaddr_p)))
        interfaces = set()
        head = ifaddr_p
        while ifaddr_p:
            interfaces.add(ifaddr_p.contents.ifa_name)
            ifaddr_p = ifaddr_p.contents.ifa_next
        libc.freeifaddrs(head) 
        return interfaces
    
    if __name__ == "__main__":
        print(get_interfaces())
    

    Do note though this method is not portable.

    0 讨论(0)
  • 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())
    
    0 讨论(0)
  • 2020-12-06 10:01

    Like David Breuer say, you can just list the directory "/ sys / class / net" on Linux. (It works on Fedora at least). If you need detailled information about some interface you can navigate on the intefaces's directories for more.

    def getAllInterfaces():
        return os.listdir('/sys/class/net/')
    
    0 讨论(0)
  • 2020-12-06 10:04

    Since this answer pops up in Google when I search for this information, I thought I should add my technique for getting the available interfaces (as well as IP addresses). The very nice module netifaces takes care of that, in a portable manner.

    0 讨论(0)
提交回复
热议问题