Last active
December 11, 2015 04:39
-
-
Save menski/4547028 to your computer and use it in GitHub Desktop.
whereami bash function - prints external ip and geoip location
This file contains hidden or 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
function whereami { | |
local apifile="$HOME/.ipinfodb" | |
if [ -f $apifile ]; then | |
curl -s --connect-timeout 1 --retry 5 "http://api.ipinfodb.com/v3/ip-city/?format=raw&key=$(cat $apifile)" | awk -F\; ' | |
$1=="OK" { | |
$0 = tolower($0) | |
printf "IP: %s\nCountry: %s (%s)\nCity: %s (%s)\nPosition: %s %s\n", $3, $5, toupper($4), $7, $6, $9, $10 | |
} | |
' | sed 's/\<./\u&/g' | |
else | |
echo "Unable to find $apifile. Please enter your api key:" | |
local apikey | |
read apikey | |
if [ -n "$apikey" ]; then | |
echo $apikey > $apifile | |
chmod 600 $apifile | |
whereami | |
else | |
echo "Please create $apifile to proceed" | |
echo " echo 'API-KEY' > $apifile && chmod 600 $apifile" | |
return 1 | |
fi | |
fi | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment