-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeolocate.sh
140 lines (125 loc) · 4.04 KB
/
geolocate.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#!/bin/ash
# https://wiki.teltonika-networks.com/view/Gsmctl_commands
# https://community.teltonika-networks.com/38925/help-with-gsmctl-commands
# https://community.teltonika-networks.com/5088/gsmctl-k-output
GoogleApiKey=""
TrackingEndpoint=""
TrackingDeviceId=""
IgnoreCache=0
DEBUG=0
CacheFile="/tmp/geolocate.cache"
serving=`gsmctl -K`
RT=`echo $serving | sed -E 's/Access tech: ([a-z]+)\b.*/\1/i'`
MCC=$((`echo $serving | sed -E 's/.*MCC: ([0-9]+)\b.*/\1/i'`) | sed 's/^0*//')
MNC=$((`echo $serving | sed -E 's/.*MNC: ([0-9]+)\b.*/\1/i'`) | sed 's/^0*//')
CELL=`gsmctl -C`
CARRIER=`gsmctl -o`
if [[ "$RT" == "LTE" ]]; then
RT="lte"
else
exit 1
fi
connected=0
wifiCurrent="00"
iwinfo=`iw dev`
wlaninterface=""
wlanssid=""
wlanaddr=""
IFS=$'\n'
for i in $iwinfo; do
MATCH=`echo $i | grep 'Interface ' | wc -c`
if [ $MATCH -ne 0 ]; then
wlanssid=""
wlanaddr=""
wlaninterface=`echo $i | sed -nE 's/.*Interface (.*)/\1/ip'`
fi
MATCH=`echo $i | grep 'addr ' | wc -c`
if [ $MATCH -ne 0 ]; then
wlanaddr=`echo $i | sed -nE 's/.*addr (.*)/\1/ip'`
fi
MATCH=`echo $i | grep 'ssid ' | wc -c`
if [ $MATCH -ne 0 ]; then
wlanssid=`echo $i | sed -nE 's/.*ssid (.*)/\1/ip'`
fi
MATCH=`echo $i | grep 'type managed' | wc -c`
if [ $MATCH -ne 0 ]; then
if [[ "$wlanssid" != "" ]]; then
wifiCurrent="$wlanaddr"
connected=1
fi
break
fi
done
cacheContent=`[ -f ${CacheFile} ] && cat ${CacheFile}`
match=`echo $cacheContent | grep '$wifiCurrent' | wc -c`
if [ $connected -ne 0 ] && [ $IgnoreCache -ne 1 ] && [ $match -ne 0 ]; then
addr=`cat ${CacheFile} | sed -n 2p`
curl $addr
exit 0
fi
wifiAccessPoints=""
wifiscan=`iw wlan0 scan`
wifiscan=`echo $wifiscan | tr '\011\012\013\014\015\040' ' '`
wifis=`echo $wifiscan | sed -r 's/BSS /\n/g'`
IFS=$'\n'
for i in $wifis; do
mac=`echo $i | sed -nE 's/.*(.{2}:.{2}:.{2}:.{2}:.{2}:.{2}).*/\1/ip'`
signalStrength=`echo $i | sed -nE 's/.*signal: (-[0-9]+).*/\1/ip'`
channel=`echo $i | sed -nE 's/.*primary channel: ([0-9]+).*/\1/ip'`
ago=`echo $i | sed -nE 's/.*last seen: ([0-9]+) ms ago.*/\1/ip'`
if [[ "$mac" != "" ]]; then
wifiAccessPoints="$wifiAccessPoints, {\"macAddress\": \"$mac\""
if [[ "$signalStrength" != "" ]]; then
wifiAccessPoints="$wifiAccessPoints,\"signalStrength\": \"$signalStrength\""
fi
if [[ "$channel" != "" ]]; then
wifiAccessPoints="$wifiAccessPoints,\"channel\": \"$channel\""
fi
if [[ "$ago" != "" ]]; then
wifiAccessPoints="$wifiAccessPoints,\"ago\": \"$ago\""
fi
wifiAccessPoints="$wifiAccessPoints }"
fi
done
wifiAccessPoints="${wifiAccessPoints:1}"
cellTowers="{ "cellId": $CELL, "mobileCountryCode": $MCC, "mobileNetworkCode": $MNC, "age": 0 }"
requestBody=$(cat <<-END
{
"homeMobileCountryCode": $MCC,
"homeMobileNetworkCode": $MNC,
"radioType": "$RT",
"carrier": "$CARRIER",
"considerIp": false,
"wifiAccessPoints": [${wifiAccessPoints}],
"cellTowers": [$cellTowers]
}
END
)
if [ $DEBUG -eq 1 ]; then
echo $requestBody
fi
# https://developers.google.com/maps/documentation/geolocation/overview
location=`curl --data "${requestBody}" -H "Content-Type: application/json" https://www.googleapis.com/geolocation/v1/geolocate?key=${GoogleApiKey}`
# echo "curl --data \"${requestBody}\" -H \"Content-Type: application/json\" https://www.googleapis.com/geolocation/v1/geolocate?key=${GoogleApiKey}"
notfound=`echo $location | sed -E 's/.*"(Not Found)".*/\1/'`
if [[ "$notfound" == "Not Found" ]]; then
echo $notfound
exit 1
fi
lat=`echo $location | sed -E 's/.*"lat": (-?[0-9]+.[0-9]+).*/\1/'`
lon=`echo $location | sed -E 's/.*"lng": (-?[0-9]+.[0-9]+).*/\1/'`
accuracy=`echo $location | sed -E 's/.*"accuracy": ([0-9]+).*/\1/'`
if [ $DEBUG -eq 1 ]; then
echo $lat
echo $lon
echo $accuracy
fi
trackingRequest="${TrackingEndpoint}/?id=${TrackingDeviceId}&lat=${lat}&lon=${lon}&accuracy=${accuracy}"
if [ $connected -eq 0 ]; then
echo $RANDOM | md5sum | head -c 20 > $CacheFile
echo '' >> $CacheFile
else
echo $wifiCurrent > $CacheFile
fi
echo $trackingRequest >> $CacheFile
curl $trackingRequest