致谢!
参考链接:

  • Python获取网卡信息(名称、MAC、
  • DingGuodong/LinuxBashShellScriptForOps

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

getNetworkStatus.py如下:

# coding:utf-8
"""
Covered on 2022-01-20
@author: Harden Qiu
# python getNetworkStatus.py
"""
import os
import systry: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 netifacesroutingGateway = 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, etcRouting Gateway:               192.168.xx.xxRouting NIC Name:              xxxxRouting NIC MAC Address:       08:71:xx:xx:xx:xxRouting IP Address:            192.168.xx.xxRouting 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_infoa, 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

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

  1. Python获取本机所有网卡的MAC地址

    在拙作<Python可以这样学>(清华大学出版社,2017.2)第297页介绍了一种获取本机网卡MAC地址的方法,不过代码显得稍微有点啰嗦,并且只能获得一块网卡的MAC地址.本文对该内容稍 ...

  2. python 获取计算机的网卡信息

    0. 前言 正常情况下,如果想要查看电脑的网卡IP地址或是MAC地址,直接通过界面找到网卡进行查看就有了,亦或是通过命令如linux的ifconfig得到IP等信息,那么本节教大家如何通过python ...

  3. Python获取局域网内所有机器IP地址与网卡MAC地址

    已发相关文章请参考Python获取本机所有网卡的MAC地址 本文代码使用ARP协议获取局域网内所有计算机的IP地址与MAC地址,思路是使用系统命令arp获取ARP表并生成文本文件,然后从文件中读取和解 ...

  4. Python获取本机所有IP地址

    众所周知,Python标准库socket中有可以获取本机IPV4地址的方法,下面是网上非常常见的一种用法: >>> import socket >>> hostna ...

  5. python获取eth0_Python获取网卡信息(名称、MAC、IP、网关等)

    "人生苦短,我用Python".Python的高效有一部分是跟它丰富的模块分不开的.Python有很多第三方模块可以帮助我们完成一些事情,减少开发时间. Python pypi库中 ...

  6. python获取mac、计算机id_python 获取本机IP、mac地址、计算机名的简单示例

    这篇文章主要为大家详细介绍了python 获取本机IP.mac地址.计算机名的简单示例,具有一定的参考价值,可以用来参考一下. 对python获取本机IP.mac地址.计算机名感兴趣的小伙伴,下面一起 ...

  7. python获取本机 ip地址、mac地址(物理地址)、计算机名、硬盘序列号、CPU序列号、主板序列号、bios序列号、bios uuid号

    文章目录 python获取本机IP地址.mac地址(物理地址).计算机名 物理地址 计算机名.ip地址 硬盘序列号 CPU序列号 主板序列号 bios序列号 bios uuid号 python获取本机 ...

  8. Python获取本地mac地址、主机名、IP地址

    1 在windows系统,命令行输入 ipconfig /all 此处获得mac地址. 2 在Python中获取mac地址 import uuid mac = uuid.UUID( int=uuid. ...

  9. python获取计算机IP、mac地址、计算机名

    在python中获取ip地址和在php中有很大不同,在php中往往比较简单.那再python中怎么做呢? 我们先来看一下python 获得本机MAC地址: import uuid def get_ma ...

最新文章

  1. java读取指定package下的所有class
  2. drop、delete与truncate的区别 和 delete是否记录日志
  3. 【Java注解系列】内置注解与AOP实现自定义注解
  4. ASP.NET MVC编程——控制器
  5. 回溯法解整数的划分问题(C语言)
  6. doc2html asp,ASP常见问题及解答(3)-ASP教程,ASP技巧
  7. 关于×××隧道技术在使用中的优势
  8. 字符串参数传递与返回值(转)
  9. bitnami redmine mysql_linux下bitnami一键安装redmine后无法远程访问mysql的问题
  10. 百练 2965 玛雅历 解题报告
  11. cad隐藏图层命令快捷键_99%的人没用过CAD图层这个功能!
  12. 微信小程序之安全调用外部API
  13. Gestalt laws
  14. windbg 常用调试命令总结
  15. STM32HAL----USB升级固件(DFU)
  16. 如何让不给听得ge乖乖听话?python教你如何做...
  17. 私域流量sop社群运营规划工作计划表格方案模板2022
  18. 大二web期末大作业——动漫海贼王(可以很好的应付老师的作业)
  19. printf中%d,%c,%s,%x各代表什么
  20. Lect2 线性分类

热门文章

  1. java web框架struts_Struts框架在Web开发中的应用
  2. Shell入门教程[1]
  3. Fatal Error[Cp001]: Copy protection check, No valid license found for this product [24]
  4. 计算机视觉:场景识别(Scene Recognition)
  5. vivo2020届春季校园招聘在线编程考试--[编程题]vivo智能手机产能
  6. 高数精髓——极限存在、连续、可导、可微和可积之间的联系
  7. python3 同时读取两个文件
  8. 整理收集46个常用区块链术语,菜鸟必看!
  9. 正能量----有时候你不逼自己一把,永远不知道自己有多优秀
  10. ubuntu16.04更改计算机名,设置 UBUNTU 16.04 的命令提示符(PS) 使其可以显示 完整的 主机名hostname...