-
Notifications
You must be signed in to change notification settings - Fork 5
/
wlan_win32.py
62 lines (50 loc) · 2.21 KB
/
wlan_win32.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
"""Get the currently connected SSID(s) from active wireless adapters
From http://stackoverflow.com/questions/2851233/how-can-i-retrieve-the-signal-strength-of-nearby-wireless-lan-networks-on-window"""
from wlan_win32_h import *
from log import log
from ctypes import *
from ctypes.wintypes import *
from sys import exit
AIRBEARS = "AirBears"
def has_airbears():
connected = get_connected_wireless()
log("Connected networks: " + str(connected))
return AIRBEARS in connected
def get_connected_wireless():
networks = []
NegotiatedVersion = DWORD()
ClientHandle = HANDLE()
ret = WlanOpenHandle(1, None, byref(NegotiatedVersion), byref(ClientHandle))
if ret != ERROR_SUCCESS:
# exit(FormatError(ret))
return
# find all wireless network interfaces
pInterfaceList = pointer(WLAN_INTERFACE_INFO_LIST())
ret = WlanEnumInterfaces(ClientHandle, None, byref(pInterfaceList))
if ret != ERROR_SUCCESS:
# exit(FormatError(ret))
return
try:
ifaces = customresize(pInterfaceList.contents.InterfaceInfo,
pInterfaceList.contents.NumberOfItems)
# find each available network for each interface
for iface in ifaces:
# print "Interface: %s" % (iface.strInterfaceDescription)
pWlanConnectionAttributes = pointer(WLAN_CONNECTION_ATTRIBUTES())
ret = WlanQueryInterface(ClientHandle, byref(iface.InterfaceGuid), wlan_intf_opcode_current_connection, None, pointer(DWORD()), byref(pWlanConnectionAttributes), None)
if ret != ERROR_SUCCESS: # Probably not connected
# exit(FormatError(ret))
continue
try:
connection_attributes = pWlanConnectionAttributes.contents
ssid = connection_attributes.wlanAssociationAttributes.dot11Ssid
ssid_string = ssid.SSID[:ssid.SSIDLength]
networks.append(ssid_string)
finally:
pass
WlanFreeMemory(pWlanConnectionAttributes)
finally:
WlanFreeMemory(pInterfaceList)
return networks
if __name__ == '__main__':
print "Connected to AirBears:", has_airbears()