Bin jetzt über ctypes und IPHLPAPI.DLL gegangen ... aber nun crasht python
Code: Alles auswählen
import ctypes
class _MIB_IFROW(ctypes.Structure):
"""
The MIB_IFROW structure stores information about a particular interface.
http://msdn.microsoft.com/en-us/library/aa366836(VS.85).aspx
{
WCHAR wszName[MAX_INTERFACE_NAME_LEN];
DWORD dwIndex;
DWORD dwType;
DWORD dwMtu;
DWORD dwSpeed;
DWORD dwPhysAddrLen;
BYTE bPhysAddr[MAXLEN_PHYSADDR];
DWORD dwAdminStatus;
DWORD dwOperStatus;
DWORD dwLastChange;
DWORD dwInOctets;
DWORD dwInUcastPkts;
DWORD dwInNUcastPkts;
DWORD dwInDiscards;
DWORD dwInErrors;
DWORD dwInUnknownProtos;
DWORD dwOutOctets;
DWORD dwOutUcastPkts;
DWORD dwOutNUcastPkts;
DWORD dwOutDiscards;
DWORD dwOutErrors;
DWORD dwOutQLen;
DWORD dwDescrLen;
BYTE bDescr[MAXLEN_IFDESCR];
} MIB_IFROW
"""
_fields_ = [
("wszName", ctypes.c_wchar_p * MAX_INTERFACE_NAME_LEN),
("dwIndex", ctypes.c_uint),
("dwType", ctypes.c_uint),
("dwMtu", ctypes.c_uint),
("dwSpeed", ctypes.c_uint),
("dwPhysAddrLen", ctypes.c_uint),
("bPhysAddr", ctypes.c_byte * MAXLEN_PHYSADDR), # XXX
("dwAdminStatus", ctypes.c_uint),
("dwOperStatus", ctypes.c_uint),
("dwLastChange", ctypes.c_uint),
("dwInOctets", ctypes.c_uint),
("dwInUcastPkts", ctypes.c_uint),
("dwInNUcastPkts", ctypes.c_uint),
("dwInDiscards", ctypes.c_uint),
("dwInErrors", ctypes.c_uint),
("dwInUnknownProtos", ctypes.c_uint),
("dwOutOctets", ctypes.c_uint),
("dwOutUcastPkts", ctypes.c_uint),
("dwOutNUcastPkts", ctypes.c_uint),
("dwOutDiscards", ctypes.c_uint),
("dwOutErrors", ctypes.c_uint),
("dwOutQLen", ctypes.c_uint),
("dwDescrLen", ctypes.c_uint),
("bDescr", ctypes.c_byte * MAXLEN_IFDESCR), # XXX
]
class _MIB_IFTABLE(ctypes.Structure):
"""
The MIB_IFTABLE structure contains a table of interface entries.
http://msdn.microsoft.com/en-us/library/aa366842(VS.85).aspx
"""
_fields_ = [
("dwNumEntries", ctypes.c_uint),
("table", _MIB_IFROW * 128),
]
def _get_network_interfaces_win32(self):
"""
Use iphlpapi.dll to retrieve interface data
http://msdn.microsoft.com/en-us/library/aa366072(VS.85).aspx
"""
print "trying iphlpapi"
iphlp = ctypes.CDLL("iphlpapi.dll")
GetIfTable = iphlp.GetIfTable # *ifTable, ulong pdwSize, bOrder
iftable = _MIB_IFTABLE()
print ".."
r = GetIfTable(iftable, ctypes.sizeof(iftable), 1)
print len(iftable)