Last active
August 26, 2016 14:00
-
-
Save psychemedia/b818a624f0da664ee743ec05c75bbadb to your computer and use it in GitHub Desktop.
Gist to detect local wifi hotspot mac addresses and geolocate them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import sys\n", | |
"print(sys.platform)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import subprocess\n", | |
"subprocess.check_output([\"/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport\", \"-s\"])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"\n", | |
"\n", | |
"import sys\n", | |
"print(sys.platform)\n", | |
"\n", | |
"#/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport\n", | |
"import subprocess\n", | |
"def getWifiMacAddresses():\n", | |
" #autodetect platform and then report based on this?\n", | |
" results = subprocess.check_output([\"/System/Library/PrivateFrameworks/Apple80211.framework/Resources/airport\", \"-s\"])\n", | |
" \n", | |
" #win?\n", | |
" #results = subprocess.check_output([\"netsh\", \"wlan\", \"show\", \"network\"])\n", | |
"\n", | |
" #linux?\n", | |
" #! apt-get -y install wireless-tools\n", | |
" #results = subprocess.check_output([\"iwlist\",\"scanning\"])\n", | |
" \n", | |
" results = results.decode(\"utf-8\") # needed in python 3\n", | |
" ls = results.split(\"\\n\")\n", | |
" ls = ls[1:]\n", | |
" macAddr={}\n", | |
" for l in [x.strip() for x in ls if x.strip()!='']:\n", | |
" ll=l.split(' ')\n", | |
" macAddr[l.strip().split(' ')[0]]=(l.strip().split(' ')[1], l.strip().split(' ')[2])\n", | |
" return macAddr\n", | |
"\n", | |
"\n", | |
"#For Mac:\n", | |
"postjson={'wifiAccessPoints':[]}\n", | |
"hotspots=getWifiMacAddresses()\n", | |
"for h in hotspots:\n", | |
" addr,db=hotspots[h]\n", | |
" postjson['wifiAccessPoints'].append({'macAddress':addr, 'signalStrength':int(db)})\n", | |
" \n", | |
"url='https://www.googleapis.com/geolocation/v1/geolocate?key={}'.format(googleMapsAPIkey)\n", | |
"\n", | |
"import requests\n", | |
"r = requests.post(url, json=postjson)\n", | |
"r.json()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"getWifiMacAddresses()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"googleMapsAPIkey=''" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"#import json\n", | |
"postjson={'wifiAccessPoints':[]}\n", | |
"hotspots=getWifiMacAddresses()\n", | |
"for h in hotspots:\n", | |
" addr,db=hotspots[h]\n", | |
" postjson['wifiAccessPoints'].append({'macAddress':addr, 'signalStrength':int(db)})\n", | |
"#json.dumps(postjson)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"url='https://www.googleapis.com/geolocation/v1/geolocate?key={}'.format(googleMapsAPIkey)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"import requests\n", | |
"r = requests.post(url, json=postjson)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"r.json()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"gl=[]\n", | |
"for h in hotspots:\n", | |
" addr,db=hotspots[h]\n", | |
" gl.append(requests.post(url, json={'macAddress':addr, 'signalStrength':int(db)}))" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"for g in gl:\n", | |
" print(g.json())" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"!pip install -U googlemaps" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"! apt-get -y install wireless-tools" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"!iwlist scanning" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true, | |
"run_control": { | |
"read_only": false | |
} | |
}, | |
"outputs": [], | |
"source": [ | |
"#http://stackoverflow.com/a/31868561/454773\n", | |
"#win\n", | |
"'''\n", | |
"import subprocess\n", | |
"results = subprocess.check_output([\"netsh\", \"wlan\", \"show\", \"network\"])\n", | |
"\n", | |
"#Adding: / mode=bssid / gives SSIDS?", | |
"#or maybe: netsh wlan show all", | |
"results = results.decode(\"ascii\") # needed in python 3\n", | |
"results = results.replace(\"\\r\",\"\")\n", | |
"ls = results.split(\"\\n\")\n", | |
"ls = ls[4:]\n", | |
"ssids = []\n", | |
"x = 0\n", | |
"while x < len(ls):\n", | |
" if x % 5 == 0:\n", | |
" ssids.append(ls[x])\n", | |
" x += 1\n", | |
"print(ssids)\n", | |
"'''" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.3" | |
}, | |
"widgets": { | |
"state": {}, | |
"version": "1.1.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment