吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1518|回复: 2
收起左侧

[已解决] python3 调用windowsAPI 获取网卡信息

[复制链接]
Cool_Breeze 发表于 2020-12-26 15:43
本帖最后由 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())

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

hobor 发表于 2020-12-26 17:46
学习一下
 楼主| Cool_Breeze 发表于 2020-12-26 23:05
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 10:42

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表