Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@talkingmoose talkingmoose commented Mar 27, 2020

image

@coasttech

This comment has been minimized.

Copy link

@coasttech coasttech commented Aug 21, 2020

This is great, thanks!

@remusache

This comment has been minimized.

Copy link

@remusache remusache commented Oct 23, 2020

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

This comment has been minimized.

Copy link
Owner Author

@talkingmoose talkingmoose commented Oct 23, 2020

@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

This comment has been minimized.

Copy link

@remusache remusache commented Oct 23, 2020

@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