Python获取网卡信息(Gateway、NIC Name、NIC MAC Address、IP Address、IP Netmask)

致谢!
参考链接:

要点:避免因uuid获取全部MAC而导致每次开机随机改变的问题。

getNetworkStatus.py如下:

# coding:utf-8
"""
Covered on 2022-01-20
@author: Harden Qiu
# python getNetworkStatus.py
"""
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]


def get_nic_info():
    """
    show Windows or Linux network Nic status, such as MAC address, Gateway, IP address, etc

    Routing Gateway:               192.168.xx.xx
    Routing NIC Name:              xxxx
    Routing NIC MAC Address:       08:71:xx:xx:xx:xx
    Routing IP Address:            192.168.xx.xx
    Routing IP Netmask:            255.255.255.0
     """
    routingNicMacAddr = ''
    routingIPAddr = ''
    routingIPNetmask = ''
    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))

    return routingGateway, routingNicName, routingNicMacAddr, routingIPAddr, routingIPNetmask

测试

# coding:utf-8
"""
Covered on 2022-01-20
@author: Harden Qiu
# python test.py
"""
from getNetworkStatus import get_nic_info


a, b, c, d, e = get_nic_info()

display_format = '%-30s %-20s'
print(display_format % ("Routing Gateway:", a))
print(display_format % ("Routing NIC Name:", b))
print(display_format % ("Routing NIC MAC Address:", c))
print(display_format % ("Routing IP Address:", d))
print(display_format % ("Routing IP Netmask:", e))

结果:

Routing Gateway:               192.168.xx.xx
Routing NIC Name:              xxxx
Routing NIC MAC Address:       08:71:xx:xx:xx:xx
Routing IP Address:            192.168.xx.xx
Routing IP Netmask:            255.255.255.0
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值