diff options
-rwxr-xr-x | geolocate | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/geolocate b/geolocate new file mode 100755 index 0000000..130d993 --- /dev/null +++ b/geolocate @@ -0,0 +1,54 @@ +#!/bin/sh -ex + +device=wlan0-1 +tmp=/tmp/geolocate_scratch + +# obtain an exclusive lock +exec 666>>/tmp/lock/geolocate +flock -w 5 -x 666; + +# list neighbour wifis +iw dev $device scan >$tmp.scan + +# construct HTTP request +( echo -n '{"request_address": true, "version": "1.1.0", "wifi_towers": [' + egrep <$tmp.scan -o -e '^BSS [0-9a-f:]{17}' -e '^[[:space:]]SSID.*' -e '^[[:space:]]signal: [0-9.-]*' \ + | tr "\n\t" " " \ + | sed 's/BSS /\n/g' \ + | tail -n +2 \ + | cut -d' ' -f1,4,7- \ + | sed 's/^\([^ ]*\) \([^ ]*\) \(.*\) $/{"mac_address":"\1", "signal_strength":"\2", "ssid":"\3"}/' \ + | tr "\n" , \ + | sed 's/,$//' + echo ']}' +) >$tmp.json +( echo "POST /loc/json HTTP/1.0 +Content-length: $(wc -c <$tmp.json) +" + cat $tmp.json +) >$tmp.request + +# consult the ooracle +nc www.google.com 80 <$tmp.request >$tmp.response + +# parse the response +tail -n1 $tmp.response \ + | tr , "\n" \ + | egrep -o -e '"latitude":[0-9.]*' -e '"longitude":[0-9.]*' -e '"street":"[^"]*' \ + | sort \ + | egrep -o '[^:"]*$' \ + | { + read lon + read lat + read street + +# store result + uci batch <<EOF +set system.position.lon=$lon +set system.position.lat=$lat +set system.position.street="$street" +EOF +} + +# clean up +rm $tmp* |