Skip to content

Instantly share code, notes, and snippets.

@MikeCook9994
Created May 31, 2016 16:16
Show Gist options
  • Save MikeCook9994/385cc133db8ae14c45c700889d3afa5f to your computer and use it in GitHub Desktop.
Save MikeCook9994/385cc133db8ae14c45c700889d3afa5f to your computer and use it in GitHub Desktop.
from sys import argv, exit
from os import popen, remove
def findBestAP(ssid, spec, privateConnect = False):
validAPs = []
with open('./dump.tmp', 'r') as f:
while True:
line = f.readline()
if not line:
break
if 'BSS' in line and '\t' not in line:
ap = []
words = line.split(' ')
BSS = words[1].split('(')
ap.append(BSS[0])
line = f.readline().strip()
protectedNetwork = False
while 'SSID' not in line:
line = f.readline().strip()
if 'freq' in line or 'signal' in line:
words = line.strip().split(' ')
ap.append(float(words[1]))
elif 'capability' in line and 'Privacy' in line and privateConnect == False:
protectedNetwork = True
words = line.split(' ')
if len(words) == 2:
ap.append(words[1])
else:
ap.append('Unknown SSID')
if ap[2] < -70 or (ssid != None and ssid != ap[3]) or protectedNetwork == True:
continue
validAPs.append(tuple(ap))
strongestSignal = -100
bestAP = None
for ap in validAPs:
BSS, freq, signal, ssid = ap
if signal > strongestSignal:
if spec == None:
strongestSignal = signal
bestAP = ap
elif spec == '2.4GHz' and freq < 3000:
strongestSginal = signal
bestAP = ap
elif spec == '5.0GHz' and freq > 3000:
strongestSginal = signal
bestAP = ap
return bestAP
def usage():
print 'Usage: python bestAP.py NIC [-s SSID] [-f frequencySpectrum] [-p privateConnect]'
print 'NIC: network interface controller'
print '\tRun ifconfig and pick one till it works (or try wlan0)'
print 'SSID: The Name of the Access Points you want to look for'
print '\tOptional argument'
print 'frequencySpectrum: the spectrum of frequency you want to connect to'
print '\tOptional argument; Valid options are \'2.4GHz\' and \'5.0GHz\''
print 'privateConnect: flag that details if you are want to be informed of private networks'
print '\tOptional flag'
if __name__ == '__main__':
if len(argv) < 2 or len(argv) > 6:
usage()
exit(0)
print 'Executing an external sudo command.'
print 'If prompted for your password, know that I\'m not trying to steal it.'
cmd = 'sudo iw dev %s scan > ./dump.tmp' % argv[1]
print cmd
popen(cmd)
ssid = None
freq = None
private = False
if '-s' in argv:
ssid = argv[argv.index('-s') + 1]
if '-f' in argv:
freq = argv[argv.index('-f') + 1]
if '-p' in argv:
private = True
bestAP = findBestAP(ssid, freq, private)
if bestAP != None:
BSS, freq, signal, SSID = bestAP
print "%s (BSSID: %s) is the best AP in your range." % (SSID, BSS)
print "frequency: %s MHz | signal Strength: %s" % (freq, signal)
else:
print "There is no available access point in your range conforming to your provided parameters."
remove('./dump.tmp')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment