Skip to content

Instantly share code, notes, and snippets.

@pjvandehaar
Created July 14, 2014 03:47
Show Gist options
  • Save pjvandehaar/3d3897db9082a284795e to your computer and use it in GitHub Desktop.
Save pjvandehaar/3d3897db9082a284795e to your computer and use it in GitHub Desktop.
sorted airport -s output
#!/usr/bin/env python3
import re, subprocess
def parse_line(line):
"returns (SECURITY, RSSI) if the line is a wifi network"
pattern = ':'.join(['[a-f0-9]{2}']*6)
reg = re.search(pattern, line)
try:
latter = line[reg.end():].split()
return (latter[-1], int(latter[0]))
except:
pass
def get_airport_s():
airport_proc = subprocess.Popen('sudo airport -s'.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
out,err = (text.decode('utf-8') for text in airport_proc.communicate(timeout=10))
except subprocess.TimeoutExpired:
airport_proc.kill()
print('TIMEOUT')
out,err = (text.decode('utf-8') for text in airport_proc.communicate())
print(out)
if err:
print('ERR:')
print(err, end='\n\n')
return out
if __name__ == '__main__':
airport_s = get_airport_s()
print('\n\nSORTED')
network_lines = []
for line in airport_s.split('\n'):
parse = parse_line(line)
if parse:
network_lines.append((line, parse))
for line in sorted(network_lines, key=lambda l:l[1]):
print(line[0])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment