-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel_graph.py
63 lines (55 loc) · 1.65 KB
/
channel_graph.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
#!/usr/bin/python
import sys
import subprocess
import os
import matplotlib.pyplot as plt
FREQ = {'5GHz': '5', '2GHz': '2'}
INTERFACE = "wlan0"
SCAN_CMD = 'iwlist {interf} | grep -w ("Channel"'
SCAN_CMD = SCAN_CMD.format(interf=INTERFACE)
def channel_graph(search_frequency):
""" plots network histogram
:search_frequency: str
'5GHz or 2GHZ'
"""
chan_list = []
proc = subprocess.Popen([SCAN_CMD], stdout=subprocess.PIPE,
shell=True)
(out, err) = proc.communicate()
lines = out.split('\n')
for line in lines:
if len(line) == 4:
channel = line[len(line) - 1]
channel = channel[0:len(channel)-1]
band = line[0]
band = band[band.find(':')+1]
if band == search_frequency:
chan_list.append(int(channel))
plt.hist(chan_list, max(chan_list) - min(chan_list))
plt.xlabel("Channels in {} GHz".format(search_frequency))
plt.ylabel("# networks")
plt.show()
def usage():
"""prints usage message, exits
"""
if len(sys.argv) != 2:
print "Usage: {} <Channel> ".format(sys.argv[0])
print "Example: channel_graph.py 2GHz"
print "Example: channel_graph.py 5GHz"
else:
print "Frequency variable can only be 2GHz or 5GHz"
print "Example: channel_graph.py 2GHz"
print "Example: channel_graph.py 5GHz"
sys.exit(1)
def main():
"""main"""
if len(sys.argv) != 2:
usage()
if sys.argv[1] == "2GHz":
channel_graph(FREQ['2GHz'])
elif sys.argv[1] == "5GHz":
channel_graph(FREQ['5GHz'])
else:
usage()
if __name__ == "__main__":
main()