#!/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 cat <