-
Notifications
You must be signed in to change notification settings - Fork 5
/
example.py
executable file
·102 lines (78 loc) · 3.1 KB
/
example.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python
'''Example usage of the wpactrl extension.'''
__author__ = 'Kel Modderman'
__copyright__ = '(c) 2008 Kel Modderman <kel@otaku42.de>'
__license__ = 'GPLv2'
import os
import sys
import time
sys.path.append('/home/blightzero/workspace/So-Fi/pywpactrl/build/lib.linux-x86_64-2.7')
import wpactrl
run = '/var/run/wpa_supplicant'
if __name__ == "__main__":
print '>>> wpactrl version %d.%d.%d ...' % wpactrl.version()
sockets = []
if os.path.isdir(run):
try:
sockets = [os.path.join(run, i) for i in os.listdir(run)]
except OSError, error:
print 'Error:', error
sys.exit(1)
if len(sockets) < 1:
print 'No wpa_ctrl sockets found in %s, aborting.' % run
sys.exit(1)
for s in sockets:
try:
print '>>> # Open a ctrl_iface connection'
print '>>> wpa = wpactrl.WPACtrl("%s")' % s
wpa = wpactrl.WPACtrl(s)
print '>>> # Location of ctrl_iface socket'
print '>>> wpa.ctrl_iface_path'
print wpa.ctrl_iface_path
print '>>> # Request a few commands'
print '>>> wpa.request("PING")'
print wpa.request('PING')
print '>>> wpa.request("STATUS")'
print wpa.request('STATUS')
print '>>> wpa.request("LIST_NETWORKS")'
print wpa.request('LIST_NETWORKS')
time.sleep(1)
print '>>> # Open a new ctrl_iface connection for receiving event'
print '>>> # messages'
print '>>> wpa_event = wpactrl.WPACtrl("%s")' % s
wpa_event = wpactrl.WPACtrl(s)
print '>>> wpa_event.attached'
print wpa_event.attached
print '>>> wpa_event.attach()'
wpa_event.attach()
print '>>> wpa_event.attached'
print wpa_event.attached
print '>>> # Request commands via original ctrl_iface connection'
print '>>> wpa.request("SCAN")'
print wpa.request('SCAN')
time.sleep(1)
print '>>> wpa.request("STATUS")'
print wpa.request('STATUS')
print '>>> # Waiting 10s for pending events ...'
time.sleep(10)
print '>>> # Check for pending events and collect them'
print '>>> while wpa_event.pending():'
print '>>> ....wpa_event.recv()'
while wpa_event.pending():
print wpa_event.recv()
print '>>> # Request scan results (wpa_supplicant only)'
print '>>> results = wpa.scanresults()'
print '>>> for no, bss in enumerate(results):'
print '>>> ....print \'bss(%d):\' % no'
print '>>> ....print bss'
results = wpa.scanresults()
for no, bss in enumerate(results):
print 'bss(%d):' % no
print bss
print '>>> # Detach the event monitor'
print '>>> wpa_event.detach()'
wpa_event.detach()
print '>>> # Finished!'
except wpactrl.error, error:
print 'Error:', error
pass