好友
阅读权限25
听众
最后登录1970-1-1
|
本帖最后由 Cool_Breeze 于 2020-12-26 20:42 编辑
缺失4个字节 198. 和 255.
不知道哪里出了问题!
解决了 ('Description', ctypes.c_char*132),
将 132 改成 128 就好了
还是不对, mac地址不对了
终于解决了!
[C] 纯文本查看 复制代码 typedef struct _IP_ADDR_STRING {
struct _IP_ADDR_STRING *Next; //1
IP_ADDRESS_STRING IpAddress; //2
IP_MASK_STRING IpMask; //3
DWORD Context; //4
} IP_ADDR_STRING,*PIP_ADDR_STRING;
我python里面定义的结构体顺序错了!(修改后正确)
[Python] 纯文本查看 复制代码 class IP_ADDR_STRING(ctypes.Structure):
pass
IP_ADDR_STRING._fields_ = [
('Next', ctypes.POINTER(IP_ADDR_STRING)), #1
('Context', ctypes.c_long), #4
('IpAddress', IP_ADDRESS_STRING), #2
('IpMask', IP_ADDRESS_STRING) #3
]
网卡名称: {6496C85D-2D3F-4D30-A659-9F90CF788B05}
网卡描述: Realtek PCIe GBE Family Controller
MAC地址 : FC-AA-14-CB-A6-F7
168.154.62
255.255.0
168.154.1
255.255.255
16.1.21
255.255.255
[Python] 纯文本查看 复制代码 #!/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())
|
|