Skip to content

Instantly share code, notes, and snippets.

@singe
Last active October 3, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singe/029076292034147823fcdfdc5ffdadde to your computer and use it in GitHub Desktop.
Save singe/029076292034147823fcdfdc5ffdadde to your computer and use it in GitHub Desktop.
Display nearby wifi network SSIDs, BSSIDs and connected clients.
#!/usr/bin/env python3
# by @singe
#
# Invoke it like you would tshark e.g.
#./wifi-hierarchy.py -r file.pcap
#./wifi-hierarchy.py -i en0
#
# Dependencies, tshark in your path and python3
from subprocess import getoutput
from sys import argv
switch = argv[1]
if switch not in ["-r","-i"]:
exit()
if switch == "-i": #Add some stuff for live capture
switch = "-a duration:2 -Ii"
cap = argv[2]
fcs='' #Frame Check Sequence check
if getoutput(f"tshark {switch} {cap} -c1 -Y wlan.fcs")!='':
fcs="-Y wlan.fcs.status==1"
ssids = [i for i in getoutput(f"tshark {switch} {cap} -T fields -e wlan_mgt.ssid {fcs} 2>/dev/null|sort -u").split('\n') if i != '']
for ssid in ssids:
print(f"SSID: {ssid}")
bssids = [i for i in getoutput(f"tshark {switch} {cap} -T fields -e wlan.bssid -Y 'wlan_mgt.ssid==\"{ssid}\"' 2>/dev/null|sort -u").split('\n') if i != 'ff:ff:ff:ff:ff:ff']
for bssid in bssids:
print(f" BSSID {bssid}")
stas = getoutput(f"tshark {switch} {cap} -T fields -e wlan.sa -Y 'wlan.sa!={bssid} && wlan.bssid=={bssid}' 2>/dev/null|sort -u").split('\n')
for sta in stas:
print(f" STA {sta}")
@singe
Copy link
Author

singe commented Oct 3, 2017

Here's an example using the cap from https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=Network_Join_Nokia_Mobile.pcap

./wifi-hierarchy.py -r Network_Join_Nokia_Mobile.pcap
SSID: martinet3
BSSID 00:01:e3:41:bd:6e
STA 00:01:e3:42:9e:2b
STA 00:15:00:34:18:52
STA 00:16:bc:3d:aa:57

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment