[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[技术讨论] python3 调用windowsAPI获取网卡信息

  1. #!/usr/bin/env python3
  2. #coding=utf-8
  3. import ctypes
  4. class IP_ADDRESS_STRING(ctypes.Structure):
  5.     _fields_ = [
  6.                     ('String', ctypes.c_char*16)
  7.                ]
  8. class IP_ADDR_STRING(ctypes.Structure):
  9.     pass
  10. IP_ADDR_STRING._fields_ = [
  11.                                ('Next', ctypes.POINTER(IP_ADDR_STRING)),
  12.                                ('IpAddress', IP_ADDRESS_STRING),
  13.                                ('IpMask', IP_ADDRESS_STRING),
  14.                                ('Context', ctypes.c_long)
  15.                           ]
  16. class _IP_ADAPTER_INFO(ctypes.Structure):
  17.     pass
  18.    
  19. _IP_ADAPTER_INFO._fields_ = [
  20.                                 ('Next', ctypes.POINTER(_IP_ADAPTER_INFO)),
  21.                                 ('ComboIndex', ctypes.c_ulong),
  22.                                 ('AdapterName', ctypes.c_char*260),
  23.                                 ('Description', ctypes.c_char*132),
  24.                                 ('AddressLength', ctypes.c_ulong),
  25.                                 ('Address', ctypes.c_ubyte*8),
  26.                                 ('Index', ctypes.c_ulong),
  27.                                 ('Type', ctypes.c_uint),
  28.                                 ('DhcpEnabled', ctypes.c_uint),
  29.                                 ('CurrentIpAddress', ctypes.POINTER(IP_ADDR_STRING)),
  30.                                 ('IpAddressList', IP_ADDR_STRING), # 该适配器的IPv4地址链表
  31.                                 ('GatewayList', IP_ADDR_STRING),
  32.                                 ('DhcpServer', IP_ADDR_STRING),
  33.                                 ('HaveWins', ctypes.c_long),
  34.                                 ('PrimaryWinsServer', IP_ADDR_STRING),
  35.                                 ('SecondaryWinsServer', IP_ADDR_STRING),
  36.                                 ('LeaseObtained', ctypes.c_longlong),
  37.                                 ('LeaseExpires', ctypes.c_longlong),
  38.                             ]
  39. dll = ctypes.windll.LoadLibrary('iphlpapi.dll')
  40. b = _IP_ADAPTER_INFO()
  41. dll.GetAdaptersInfo(ctypes.byref(b),
  42.                     ctypes.byref(ctypes.c_ulong(ctypes.sizeof(b))))
  43. print('网卡名称:', b.AdapterName.decode())
  44. print('网卡描述:', b.Description.decode())
  45. # print(b.DhcpEnabled)
  46. # print(b.Type) # 6 以太网接口
  47. print("MAC地址 : "
  48.       f"{b.Address[0]:02X}-{b.Address[1]:02X}-{b.Address[2]:02X}-"
  49.       f"{b.Address[3]:02X}-{b.Address[4]:02X}-{b.Address[5]:02X}")
  50. print(b.IpAddressList.IpAddress.String.decode())
  51. print(b.IpAddressList.IpMask.String.decode())
  52. print()
  53. print(b.GatewayList.IpAddress.String.decode())
  54. print(b.GatewayList.IpMask.String.decode())
  55. print()
  56. print(b.DhcpServer.IpAddress.String.decode())
  57. print(b.DhcpServer.IpMask.String.decode())
复制代码
网卡名称: {6496C85D-2D3F-4D30-A659-9F90CF788B05}
网卡描述: Realtek PCIe GBE Family Controller
MAC地址 : FC-AA-14-CB-A6-F7
192.168.154.62
255.255.255.0

192.168.154.1
255.255.255.255

172.16.1.21
255.255.255.25

回复 1# Gin_Q

在我的机器上这句话报错。

print("MAC地址 : "
      f"{b.Address[0]:02X}-{b.Address[1]:02X}-{b.Address[2]:02X}-"
      f"{b.Address[3]:02X}-{b.Address[4]:02X}-{b.Address[5]:02X}")

TOP

回复 2# netdzb


    有bug , 有些系统用不了!

TOP

返回列表