Skip to content

Instantly share code, notes, and snippets.

@talkingmoose
Last active March 29, 2023 19:42
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talkingmoose/7d1bf4f884ca08f95fd3baf0014fc639 to your computer and use it in GitHub Desktop.
Save talkingmoose/7d1bf4f884ca08f95fd3baf0014fc639 to your computer and use it in GitHub Desktop.
Add the following script to a Jamf Pro extension attribute to collect service provider location information based on public IP address when updating inventory.
#!/bin/zsh
# provide for Big Sur and earlier
xpath() {
# the xpath tool changes in Big Sur
if [[ $( /usr/bin/sw_vers -buildVersion) > "20A" ]]; then
/usr/bin/xpath -e "$@"
else
/usr/bin/xpath "$@"
fi
}
# get public IP address
publicIP=$( /usr/bin/curl http://ifconfig.me/ip \
--location \
--silent \
--max-time 10 )
# get GeoIP data
locationData=$( /usr/bin/curl http://ip-api.com/xml/$publicIP \
--location \
--silent \
--max-time 10 )
locationPieces=( country countryCode region regionName city zip lat lon timezone isp org as )
for anItem in $locationPieces
do
export $anItem="$( xpath "/query/$anItem/text()" 2>/dev/null <<< "$locationData" )"
done
echo "<result>$country
$countryCode
$region
$regionName
$city
$zip
$lat
$lon
$timezone
$isp
$org
$as</result>"
exit 0
@talkingmoose
Copy link
Author

image

@coasttech
Copy link

This is great, thanks!

@remusache
Copy link

Thank you so much for the script!
Works great in Catalina.

Is it just me or the script is not working in Big Sur. I get no results…

@talkingmoose
Copy link
Author

@remusache, I was recently made aware of an xpath change in Big Sur. The script will need to be tweaked a little. See this for information about what to do.

https://scriptingosx.com/2020/10/dealing-with-xpath-changes-in-big-sur/

I won't be able to test and confirm this change for a little while.

@remusache
Copy link

@talkingmoose Thank you so much for the quick feedback!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment