forked from iancoleman/python-iwlist
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiwlist.py
More file actions
Latest commit
56 lines (52 loc) · 2.18 KB
/
Copy pathiwlist.py
File metadata and controls
56 lines (52 loc) · 2.18 KB
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
importre
importsubprocess
cellNumberRe=re.compile(r"^Cell\s+(?P<cellnumber>.+)\s+-\s+Address:\s(?P<mac>.+)$")
regexps= [
re.compile(r"^ESSID:\"(?P<essid>.*)\"$"),
re.compile(r"^Protocol:(?P<protocol>.+)$"),
re.compile(r"^Mode:(?P<mode>.+)$"),
re.compile(r"^Frequency:(?P<frequency>[\d.]+) (?P<frequency_units>.+)( \(Channel \d+\))?$"),
re.compile(r"^Channel:(?P<channel>\d+)$"),
re.compile(r"^Encryption key:(?P<encryption>.+)$"),
re.compile(r"^Quality=(?P<signal_quality>\d+)/(?P<signal_total>\d+)\s+Signal level=(?P<signal_level_dBm>.+) d.+$"),
re.compile(r"^Signal level=(?P<signal_quality>\d+)/(?P<signal_total>\d+).*$"),
]
# Detect encryption type
wpaRe=re.compile(r"IE:\ WPA\ Version\ 1$")
wpa2Re=re.compile(r"IE:\ IEEE\ 802\.11i/WPA2\ Version\ 1$")
# Runs the comnmand to scan the list of networks.
# Must run as super user.
# Does not specify a particular device, so will scan all network devices.
defscan(interface='wlan0'):
cmd= ["iwlist", interface, "scan"]
proc=subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
points=proc.stdout.read().decode('utf-8')
returnpoints
# Parses the response from the command "iwlist scan"
defparse(content):
cells= []
lines=content.split('\n')
forlineinlines:
line=line.strip()
cellNumber=cellNumberRe.search(line)
ifcellNumberisnotNone:
cells.append(cellNumber.groupdict())
continue
wpa=wpaRe.search(line)
ifwpaisnotNone :
cells[-1].update({'encryption':'wpa'})
wpa2=wpa2Re.search(line)
ifwpa2isnotNone :
cells[-1].update({'encryption':'wpa2'})
forexpressioninregexps:
result=expression.search(line)
ifresultisnotNone:
if'encryption'inresult.groupdict() :
ifresult.groupdict()['encryption'] =='on' :
cells[-1].update({'encryption': 'wep'})
else :
cells[-1].update({'encryption': 'off'})
else :
cells[-1].update(result.groupdict())
continue
returncells