How to get Network Interface Card names in Python?

后端 未结 9 1793
灰色年华
灰色年华 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 10:11

    I don't think there's anything in the standard library to query these names.

    If I needed these names on a Linux system I would parse the output of ifconfig or the contents of /proc/net/dev. Look at this blog entry for a similar problem.

    0 讨论(0)
  • 2020-12-06 10:11

    There is a python package get-nic which gives NIC status, up\down, ip addr, mac addr etc

    
    pip install get-nic
    
    from get_nic import getnic
    
    getnic.interfaces()
    
    Output: ["eth0", "wlo1"]
    
    interfaces = getnic.interfaces()
    getnic.ipaddr(interfaces)
    
    Output: 
    {'lo': {'state': 'UNKNOWN', 'inet4': '127.0.0.1/8', 'inet6': '::1/128'}, 'enp3s0': {'state': 'DOWN', 'HWaddr': 'a4:5d:36:c2:34:3e'}, 'wlo1': {'state': 'UP', 'HWaddr': '48:d2:24:7f:63:10', 'inet4': '10.16.1.34/24', 'inet6': 'fe80::ab4a:95f7:26bd:82dd/64'}}
    

    Refer GitHub page for more information: https://github.com/tech-novic/get-nic-details

    0 讨论(0)
  • 2020-12-06 10:13

    To add to what @Kristian Evensen's mentions, here is what I used for a problem i was having. If you are looking to just get a list of the interfaces, use:

    interface_list = netifaces.interfaces()
    

    If you are wanting a specific interface, but don't know what the number at the end is (ie: eth0), use:

    interface_list = netifaces.interfaces()
    interface = filter(lambda x: 'eth' in x,interface_list)
    
    0 讨论(0)
提交回复
热议问题