Skip to content

Instantly share code, notes, and snippets.

@colemar
Last active November 21, 2022 14:58
Show Gist options
  • Save colemar/727125dea3d17543b1099ccff84f6eda to your computer and use it in GitHub Desktop.
Save colemar/727125dea3d17543b1099ccff84f6eda to your computer and use it in GitHub Desktop.
Free ip address geolocation with bash
sudo wget https://gist.github.com/colemar/727125dea3d17543b1099ccff84f6eda/raw/88dd98c6283d9586248e9644023b4bb37ee1799f/gil -o /usr/local/bin/gil
sudo chmod +x /usr/local/bin/gil
#!/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)"
@colemar
Copy link
Author

colemar commented Nov 21, 2022

Free in the sense of gratis.
Please don't abuse free services.

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