Last active
November 21, 2022 14:58
-
-
Save colemar/727125dea3d17543b1099ccff84f6eda to your computer and use it in GitHub Desktop.
Free ip address geolocation with bash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sudo wget https://gist.github.com/colemar/727125dea3d17543b1099ccff84f6eda/raw/88dd98c6283d9586248e9644023b4bb37ee1799f/gil -o /usr/local/bin/gil | |
sudo chmod +x /usr/local/bin/gil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
error_exit () { | |
echo -e "error: $1" >&2 | |
exit 1 | |
} | |
(( $# == 1 )) \ | |
&& [[ "${1}." =~ ^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])[.]){4}$ ]] \ | |
|| error_exit "syntax\n${0} ipaddress\nwhere 123.0.13.171 is an example of valid ipaddress, while 123.5.013.171 is not" | |
for cmd in curl jq; do | |
command -v $cmd >/dev/null || error_exit "missing command $cmd" | |
done | |
curl -s http://api.db-ip.com/v2/free/${1} | jq | |
resp=$(curl -s http://ipinfo.io/${1}) | |
jq <<<$resp | |
echo "https://www.google.com/maps/place/$(jq -r '.loc' <<<$resp)" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Free in the sense of gratis.
Please don't abuse free services.