-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocate.sh
executable file
·89 lines (61 loc) · 2.19 KB
/
locate.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/env sh
# author: Tegan Burns
# website: teganburns.com
# Check README.md for details on how to get your API key
api_key="AIzaSyBwM79ERbyDMs_CVWt7T9DHxKAYvUmBHlk"
url="https://www.googleapis.com/geolocation/v1/geolocate?key=$api_key"
json_file="result.json"
jqobj=""
# Remove old json_file
if [[ -e $json_file ]]; then
rm $json_file
fi
#TODO: This may run into issues with mutiple WiFi devices
device=$( iw dev | sed '/Interface.*$/!d' | sed -e 's/^.*Interface //' )
scan=$( iw dev $device scan | sed -e 's/[ \t]*//' -e 's/[ \t]*$//' )
#BSS: xx:xx:xx:xx:xx:xx(dev whatever)
#signal: -77.00 dBm
#last seen: 1236 ms ago
#DS Parameter set: channel 36
# Filter out queries with sed
macAddr=($(echo "$scan" | sed -r -n 's/BSS ((([0-9a-fA-F]{2}):){5}([0-9a-fA-F]){2}).*$/\1/p' ))
signal=($(echo "$scan" | sed -r -n 's/signal: (-?[0-9]+)\.[0-9]* dBm/\1/p' ))
age=($(echo "$scan" | sed -r -n 's/last seen: ([0-9]*).*$/\1/p' ))
channel=($( echo "$scan" | sed -r -n 's/DS Parameter set: channel ([0-9]+)*.$/\1/p' | sed -r -e 's/^$/NULL/p'))
# Put all entries in JSON file
for i in ${!macAddr[@]}; do
jqobj+="\"macAddress\": \"${macAddr[$i]}\", "
jqobj+="\"signalStrength\": ${signal[$i]}, "
# Ignore NULL key
if [[ ${channel[$i]} == "NULL" ]]; then
jqobj+="\"age\": ${age[$i]}"
else
jqobj+="\"age\": ${age[$i]}, "
jqobj+="\"channel\": ${channel[$i]}"
fi
# Create new file if we need to
if [[ ! -e $json_file ]]; then
echo "{ \"considerIp\": \"false\", \"wifiAccessPoints\": [" > $json_file
fi
# If Last Obj for JSON file
if [[ $i == $(( ${#macAddr[@]} -1 )) ]]; then
echo "{$jqobj}]}" >> $json_file
else
echo "{$jqobj}," >> $json_file
fi
jqobj=""
continue;
done
# Make Request
result=$(curl -s -d @$json_file -H "Content-Type: application/json" $url)
lat=$(echo "$result" | jq ' .location.lat')
lng=$(echo "$result" | jq ' .location.lng')
acc=$( echo "$result" | jq '.accuracy' )
# Print result or handle error
if [[ $lat == "null" || $lng == "null" ]]; then
echo "Error: $result"
else
echo "Estimated Accuracy: $acc meters"
echo "https://www.google.com/maps/search/?api=1&query=$lat,$lng"
fi
exit