Skip to content

Instantly share code, notes, and snippets.

@smmtaheri
Created October 5, 2018 18:36
Show Gist options
  • Save smmtaheri/960d0371aa2f171dc24baffcb483654b to your computer and use it in GitHub Desktop.
Save smmtaheri/960d0371aa2f171dc24baffcb483654b to your computer and use it in GitHub Desktop.
location of ip
#!/bin/bash
# Bash function to find location of an ip
# Example:
# $ locof 8.8.8.8
# US
# find your location when nothing is sent
# $ locof
# IR
# -c option for city
# -i option for isp
# -l option for geo location
# -r option for region
# $ locof -ircl
# US
# Piscataway
# New Jersey
# AS55081 24 SHELLS
# 40.5516,-74.4637
function locof() {
if [ -z "$1" ]; then
curl ipinfo.io/country
else
if [ -z "$2" ]; then
if [[ $1 == "-"* ]]; then
out="$(curl ipinfo.io/country 2>/dev/null)"
if [[ $1 == *"c"* ]]; then
city="$(curl ipinfo.io/city 2>/dev/null)"
out="$out\n$city"
fi
if [[ $1 == *"r"* ]]; then
region="$(curl ipinfo.io/region 2>/dev/null)"
out="$out\n$region"
fi
if [[ $1 == *"i"* ]]; then
isp="$(curl ipinfo.io/org 2>/dev/null)"
out="$out\n$isp"
fi
if [[ $1 == *"l"* ]]; then
loc="$(curl ipinfo.io/loc 2>/dev/null)"
out="$out\n$loc"
fi
echo -e $out
else
curl ipinfo.io/$1/country
fi
else
out="$(curl ipinfo.io/$2/country 2>/dev/null)"
if [[ $1 == *"c"* ]]; then
city="$(curl ipinfo.io/$2/city 2>/dev/null)"
out="$out\n$city"
fi
if [[ $1 == *"r"* ]]; then
region="$(curl ipinfo.io/$2/region 2>/dev/null)"
out="$out\n$region"
fi
if [[ $1 == *"i"* ]]; then
isp="$(curl ipinfo.io/$2/org 2>/dev/null)"
out="$out\n$isp"
fi
if [[ $1 == *"l"* ]]; then
loc="$(curl ipinfo.io/$2/loc 2>/dev/null)"
out="$out\n$loc"
fi
echo -e $out
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment