zoukankan      html  css  js  c++  java
  • python获取网卡相关信息

    python获取网卡信息

    pip install netifaces
    
    import os
    import sys
    
    try:
        import netifaces
    except ImportError:
        try:
            command_to_execute = "pip install netifaces || easy_install netifaces"
            os.system(command_to_execute)
        except OSError:
            print "Can NOT install netifaces, Aborted!"
            sys.exit(1)
        import netifaces
    
    routingGateway = netifaces.gateways()['default'][netifaces.AF_INET][0]
    routingNicName = netifaces.gateways()['default'][netifaces.AF_INET][1]
    
    for interface in netifaces.interfaces():
        if interface == routingNicName:
            # print netifaces.ifaddresses(interface)
            routingNicMacAddr = netifaces.ifaddresses(interface)[netifaces.AF_LINK][0]['addr']
            try:
                routingIPAddr = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']
                # TODO(Guodong Ding) Note: On Windows, netmask maybe give a wrong result in 'netifaces' module.
                routingIPNetmask = netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['netmask']
            except KeyError:
                pass
    
    display_format = '%-30s %-20s'
    print display_format % ("Routing Gateway:", routingGateway)
    print display_format % ("Routing NIC Name:", routingNicName)
    print display_format % ("Routing NIC MAC Address:", routingNicMacAddr)
    print display_format % ("Routing IP Address:", routingIPAddr)
    print display_format % ("Routing IP Netmask:", routingIPNetmask)
    """
    odin@odin:~$ python test1.py 
    Routing Gateway:               192.168.1.1         
    Routing NIC Name:              br0                 
    Routing NIC MAC Address:       ec:d6:8a:5d:bf:d6   
    Routing IP Address:            192.168.1.121       
    Routing IP Netmask:            255.255.255.0 
    """
    
  • 相关阅读:
    为什么富人越来越富,穷人越来越穷?
    计算几何基础_点_向量_极角排序
    滑窗模板_双向队列
    后缀数组
    AC自动机
    RMQ_ST表
    二叉树求逆序对(伪AC 23333)
    分块
    莫队
    树状数组_二维
  • 原文地址:https://www.cnblogs.com/qianzhengkai/p/13537089.html
Copyright © 2011-2022 走看看