-
Notifications
You must be signed in to change notification settings - Fork 0
/
iwlist.py
executable file
·61 lines (54 loc) · 2.22 KB
/
iwlist.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
"""
iwlist extracted from https://github.com/iancoleman/python-iwlist
"""
import re
import subprocess
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 (?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 command to scan the list of networks.
# Must run as super user.
# Does not specify a particular device, so will scan all network devices.
def scan(interface='wlan0'):
cmd = ["iwlist", interface, "scan"]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
points = proc.stdout.read().decode('utf-8')
return points
# Parses the response from the command "iwlist scan"
def parse(content):
cells = []
lines = content.split('\n')
for line in lines:
line = line.strip()
cell_number = cellNumberRe.search(line)
if cell_number is not None:
cells.append(cell_number.groupdict())
continue
wpa = wpaRe.search(line)
if wpa is not None:
cells[-1].update({'encryption': 'wpa'})
wpa2 = wpa2Re.search(line)
if wpa2 is not None:
cells[-1].update({'encryption': 'wpa2'})
for expression in regexps:
result = expression.search(line)
if result is not None:
if 'encryption' in result.groupdict():
if result.groupdict()['encryption'] == 'on':
cells[-1].update({'encryption': 'wep'})
else:
cells[-1].update({'encryption': 'off'})
else:
cells[-1].update(result.groupdict())
continue
return cells