Skip to content

Instantly share code, notes, and snippets.

@3m3x
Created October 19, 2019 14:18
Show Gist options
  • Save 3m3x/dabc37b68b6acbd1c903ab766369ca57 to your computer and use it in GitHub Desktop.
Save 3m3x/dabc37b68b6acbd1c903ab766369ca57 to your computer and use it in GitHub Desktop.
Show detailed info about the surrounding wifi networks
#!/usr/bin/env python
#
# Needs root priv to run
#
from pprint import pprint as pp
import re
import subprocess
import sys
def peek(l):
if l[-1].strip().startswith('*'):
return True
else:
return False
def make_wifi_info_subdict(l):
name_value_regex = r'\*\s(.+): (.+)'
value_regex = r'\*\s(.+)'
new_dict = dict()
new_list = list()
while peek(l):
line = l.pop().strip()
if re.search(name_value_regex, line):
name, value = re.search(name_value_regex, line).groups()
name = name.lower().replace(' ', '_')
new_dict[name] = value
elif re.search(value_regex, line):
new_list.extend(re.search(value_regex, line).groups())
else:
raise Exception(line)
if new_dict:
return new_dict
elif new_list:
return new_list
if __name__ == '__main__':
wifi_networks = {}
if len(sys.argv) < 2:
print("usage: ./wifi-info.py <wifi-interface>")
exit(-1)
else:
wifi_iface = sys.argv[1]
data = subprocess.check_output(["iw", "dev", wifi_iface, "scan"]).split('\n')
data.reverse()
#import ipdb; ipdb.set_trace()
while data:
line = data.pop().strip()
if re.search(r'BSS [a-f\d:]+', line):
current_wifi = dict()
wifi_networks[line[4:21]] = current_wifi
elif 'SSID' in line:
current_wifi['ssid'] = line[6:]
elif line.startswith('signal:'):
current_wifi['signal_strength'] = line[8:]
elif line.startswith('WPS'):
current_wifi['WPS'] = make_wifi_info_subdict(data)
current_wifi['WPS']['version'] = line[line.rfind('Version') + 9:]
elif line.startswith('RSN'):
current_wifi['RSN'] = make_wifi_info_subdict(data)
current_wifi['RSN']['version'] = line[line.rfind('Version') + 9:]
elif line.startswith('WPA'):
current_wifi['WPA'] = make_wifi_info_subdict(data)
current_wifi['WPA']['version'] = line[line.rfind('Version') + 9:]
elif line.startswith('Extended capabilities'):
current_wifi['extended_capabilities'] = make_wifi_info_subdict(data)
pp(wifi_networks)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment