I wrote this extension attribute to do a Geo lookup on the external IP address of our laptops. It uses the API from IPStack and stores the last recorded IP address locally, only doing a new lookup if the IP address changes. This prevents extra lookups. You can also add multiple API keys and it will randomly select one for the request.
If it returns your organisations external IP then you can customise what the extension attribute response will be, in this case I added the school name to make it easier for our support staff to see the device was on prem.
You can also specify where you want the two text files written to. /Users/Shared/ is the default location.
My sed skills could do with a little polishing, but it gets the job done :)
#!/bin/sh
# Script to get Geo location data from dyndns.org and IP Stack
# Will only check if a new IP is recorded, will update manually if school IP is detected
# Written 01/04/2019
# Jacob Curulli - ILT Manager, Tranby College
# Edit these values below
# Create an array of API keys and randomly select one to use
declare -a API=('123' '123' '123')
index=$( jot -r 1 0 $((${#API[@]} - 1)) )
APIKey=${API[index]}
# School external facing IP address
schoolIP="127.0.0.1"
# School location information
schoolLocation="Tranby College, Perth, Australia : -31.9674, 115.8621"
# File location to save text files to
fileLocation="/Users/Shared/"
#Get current IP address my DynDNS
currentIP=`curl -L -s --max-time 10 http://checkip.dyndns.org | egrep -o -m 1 '([[:digit:]]{1,3}.){3}[[:digit:]]{1,3}'`
# Read in last recorded IP address
oldIP=`cat "$fileLocation".ip.txt`
#Read in last recorded location
oldLocation=`cat "$fileLocation".location.txt`
echo "Current IP is $currentIP"
echo "Selected API Key is $APIKey"
echo "Old IP is $oldIP"
echo "Old location is $oldLocation"
if [ "$currentIP" = "$oldIP" ];
then
echo "IP's are the same nothing to do"
elif [ "$currentIP" = "$schoolIP" ];
then
# Device is at school, No need to waste an IP Stack lookup
echo "IP matches school IP, manually updating location"
echo "Writing current IP to disk - $currentIP"
echo $currentIP > "$fileLocation".ip.txt
echo "Writing current location to disk - $myLocation"
echo $myLocation > "$fileLocation".location.txt
echo "<result>$schoolLocation</result>"
else
echo "There is an IP mismatch"
# Fetch GEO location info from IPStack using IP address
myLocationInfo=`curl -L -s --max-time 10 http://api.ipstack.com/$currentIP?access_key="$APIKey"&format=0`
myCountryName=`echo $myLocationInfo | awk -F, '{print $6 }' | sed 's/.*://g' | sed 's/"//g'`
myRegionCode=`echo $myLocationInfo | awk -F, '{print $7 }' | sed 's/.*://g' | sed 's/"//g'`
myCity=`echo $myLocationInfo | awk -F, '{print $9 }' | sed 's/.*://g' | sed 's/"//g'`
myLatitude=`echo $myLocationInfo | awk -F, '{print $11 }' | sed 's/.*://g'`
myLongitude=`echo $myLocationInfo | awk -F, '{print $12 }' | sed 's/.*://g'`
myLocation=`echo "$myCity, $myCountryName : $myLatitude, $myLongitude"`
echo "Writing current IP to disk - $currentIP"
echo $currentIP > "$fileLocation".ip.txt
echo "Writing current location to disk - $myLocation"
echo $myLocation > "$fileLocation".location.txt
echo "<result>$myLocation</result>"
fi