标题: [技术讨论] python3 调用windowsAPI获取网卡信息 [打印本页]
作者: Gin_Q 时间: 2020-12-26 20:43 标题: python3 调用windowsAPI获取网卡信息
- #!/usr/bin/env python3
- #coding=utf-8
-
-
- import ctypes
-
-
- class IP_ADDRESS_STRING(ctypes.Structure):
- _fields_ = [
- ('String', ctypes.c_char*16)
- ]
-
- class IP_ADDR_STRING(ctypes.Structure):
- pass
-
- IP_ADDR_STRING._fields_ = [
- ('Next', ctypes.POINTER(IP_ADDR_STRING)),
- ('IpAddress', IP_ADDRESS_STRING),
- ('IpMask', IP_ADDRESS_STRING),
- ('Context', ctypes.c_long)
- ]
-
- class _IP_ADAPTER_INFO(ctypes.Structure):
- pass
-
- _IP_ADAPTER_INFO._fields_ = [
- ('Next', ctypes.POINTER(_IP_ADAPTER_INFO)),
- ('ComboIndex', ctypes.c_ulong),
- ('AdapterName', ctypes.c_char*260),
- ('Description', ctypes.c_char*132),
- ('AddressLength', ctypes.c_ulong),
- ('Address', ctypes.c_ubyte*8),
- ('Index', ctypes.c_ulong),
- ('Type', ctypes.c_uint),
- ('DhcpEnabled', ctypes.c_uint),
- ('CurrentIpAddress', ctypes.POINTER(IP_ADDR_STRING)),
- ('IpAddressList', IP_ADDR_STRING), # 该适配器的IPv4地址链表
- ('GatewayList', IP_ADDR_STRING),
- ('DhcpServer', IP_ADDR_STRING),
- ('HaveWins', ctypes.c_long),
- ('PrimaryWinsServer', IP_ADDR_STRING),
- ('SecondaryWinsServer', IP_ADDR_STRING),
- ('LeaseObtained', ctypes.c_longlong),
- ('LeaseExpires', ctypes.c_longlong),
- ]
-
- dll = ctypes.windll.LoadLibrary('iphlpapi.dll')
- b = _IP_ADAPTER_INFO()
-
- dll.GetAdaptersInfo(ctypes.byref(b),
- ctypes.byref(ctypes.c_ulong(ctypes.sizeof(b))))
-
-
- print('网卡名称:', b.AdapterName.decode())
- print('网卡描述:', b.Description.decode())
- # print(b.DhcpEnabled)
- # print(b.Type) # 6 以太网接口
- 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}")
-
-
- print(b.IpAddressList.IpAddress.String.decode())
- print(b.IpAddressList.IpMask.String.decode())
- print()
- print(b.GatewayList.IpAddress.String.decode())
- print(b.GatewayList.IpMask.String.decode())
- print()
- print(b.DhcpServer.IpAddress.String.decode())
- 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
作者: netdzb 时间: 2020-12-27 09:27
回复 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}")
作者: Gin_Q 时间: 2020-12-28 09:08
回复 2# netdzb
有bug , 有些系统用不了!
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |