Skip to content

Instantly share code, notes, and snippets.

@pyropeter
Created May 13, 2019 12:52
Show Gist options
  • Save pyropeter/98e71cfc11494cfde11411df35faef11 to your computer and use it in GitHub Desktop.
Save pyropeter/98e71cfc11494cfde11411df35faef11 to your computer and use it in GitHub Desktop.
Use wpa_cli to print hostnames of nearby cisco APs
from subprocess import run, CalledProcessError
from time import sleep
CMD = ["wpa_cli"]
def scan_results(_scan=True):
res = []
proc = run(CMD + ["scan_results"],
check=True, capture_output=True)
for line in proc.stdout.splitlines():
if line[2] != ord(':') or line[5] != ord(':'):
continue
line = line.split(b'\t')
res.append((
line[0].decode('ascii'),
int(line[1])/1000,
int(line[2]),
line[3].decode('ascii'),
line[4].decode('ascii')))
if len(res) == 1 and _scan:
run(CMD + ['scan'], check=True, capture_output=True)
sleep(10)
return scan_results(_scan=False)
return res
def status():
proc = run(CMD + ['status'], check=True, capture_output=True)
x = filter(lambda x: x[:3] == b'bssid=', proc.stdout.splitlines())
return next(x)[6:].decode('ascii')
def get_ie(bssid):
proc = run(CMD + ['bss', bssid], check=True, capture_output=True)
x = filter(lambda x: x[:3] == b'ie=', proc.stdout.splitlines())
x = next(x)[3:]
ie = []
while x:
ie.append(int(x[:2], 16))
x = x[2:]
return bytes(ie)
def parse_ie(ie):
res = []
while ie:
t = ie[0]
l = ie[1]
v = ie[2:2+l]
ie = ie[2+l:]
res.append((t, v))
return res
def parse_schissko(buf):
name = buf[10:25].decode('ascii')
# indicate omission
if name[-1] != '\0':
name += '…'
# remove padding
while name[-1] == '\0':
name = name[:-1]
return name
hexstr = lambda x: ' '.join(["%02x" % y for y in x])
for bssid, freq, signal, flags, ssid in scan_results():
ies = parse_ie(get_ie(bssid))
for value in filter(lambda x: x[0] == 133, ies):
print("%s\t%3.1f\t%5i\t%s" % (
bssid, freq, signal, parse_schissko(value[1])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment