Skip to content

Instantly share code, notes, and snippets.

@drygdryg
Created March 29, 2021 14:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drygdryg/21b2045a9577738456131dce84a9e889 to your computer and use it in GitHub Desktop.
Save drygdryg/21b2045a9577738456131dce84a9e889 to your computer and use it in GitHub Desktop.
Extract info about a single access point from iw scan output
#!/usr/bin/env python3
import subprocess
import sys
import re
BSS_PATTERN = re.compile(r'BSS (\S+)( )?\(on \w+\)')
if __name__ == '__main__':
if len(sys.argv) != 3:
print(f"Usage: {sys.argv[0]} <interface> <bssid>")
exit(1)
interface = sys.argv[1]
bssid = sys.argv[2].lower()
cmd = 'iw dev {} scan'.format(interface)
proc = subprocess.run(cmd, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, encoding='utf-8', errors='ignore')
lines = proc.stdout.splitlines()
result = ""
current_bssid = ""
for line in lines:
match = re.match(BSS_PATTERN, line)
if match:
current_bssid = match.group(1)
if current_bssid == bssid:
result += line + '\n'
result = result[:-1] # Skip last newline
if result:
print(result)
else:
print("No results")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment