Gizmo's Freeware Forum

Gizmo's Freeware Forum (
-   Linux (
-   -   IP Location script: (

debtboy 12. Dec 2009 01:27 AM

IP Location script:
I made a simple little bash script (using a web API) which exposes the location (down to the nearest city) of any IP address.
You see similar things on websites written in javascript, but I figured someone might
benefit from seeing it as a shell script with a brief explanation.

Here is the script:


echo ""
echo "Enter an IP Address or leave blank for your IP: "
read address

if [ "$address" == "" ]; then

var_xml=`wget -q $var_url -O -`

var_ip=`echo $var_xml | grep -o "<Ip>[0-9 \.]*<\/Ip>" | \
    sed -e 's/<Ip>//' -e 's/<\/Ip>//'`

var_country=`echo $var_xml | grep -o "<CountryName>[A-Z a-z \s]*<\/CountryName>" | \
    sed -e 's/<CountryName>//' -e 's/<\/CountryName>//'`

var_state=`echo $var_xml | grep -o "<RegionName>[A-Z a-z \s]*<\/RegionName>" | \
    sed -e 's/<RegionName>//' -e 's/<\/RegionName>//'`

var_city=`echo $var_xml | grep -o "<City>[A-Z a-z \s]*<\/City>" | \
    sed -e 's/<City>//' -e 's/<\/City>//'`

echo ""
echo "IP Address:  $var_ip"
echo "Country:      $var_country"
echo "State/Region: $var_state"
echo "City:        $var_city"
echo ""

I'll give a brief explanination then a simple demonstration.
The first line #!/bin/bash is called she-bang it informs the kernel to use a bash shell to interpret this file.

echo sends things to the standard output, if not redirected (the screen in this case).
read accepts input from the command line when the <enter> key is pressed.

If was used to determine if an IP address was entered on the command line or if it was left blank.
If it was left blank, the API will use your IP address as input
(your ip address is always passed to a server during a request).

The var_url variable now holds the web API (automatic programming interface) address.

wget goes to the web address and returns the response which in our case was assigned to the var_xml variable.

In the next 4 lines, you can see where I pipe "|" the variable contents into grep searching for particular patterns
via regex (Regular Expressions) and then parse (strip off) the xml tags
via sed (stream editor) again using regex (Regular Expressions).
Regex is very powerful, but not so easy to explain. ;)
Google will help if your interested.

The last section echoes some output to the screen. I send a text label or description and then the contents of each variable.

No lets see it in action... :D

debtboy 12. Dec 2009 01:40 AM

Now for a simple demonstration...

If your going to use this file, copy it into a text editor and save it.
I saved it as, but you name it whatever you want.
In addition to saving the file, you will have to make it executable, use this command:

chmod 755 ./
The "./" just means the current directory you are in and if you named
the file something different then adjust the command accordingly.

I'm going to pick an IP address we are all familiar with (this website) Ping should do the trick.

I'm running ping for 1 count which is enough to expose the IP address.

Now to run the script...

debtboy 12. Dec 2009 01:57 AM

Once we hit enter, the script is running, the first line should look familiar,
you can refer back to the script and see the echo.

Now I type in the "" IP address, we found earlier
and the location (down to the nearest city) is given.

If you don't type an IP address and just hit enter, the location of your pc/gateway (down to the nearest city) will be displayed.

This was a very simple, yet powerful script.
There are over 5,000 commands on a typical Linux system,
each with various options which allow you to do just about anything.
When used in conjunction with cron/crontab you can do
just about anything without even being there (Ha! Ha! Ha!) :D

If your not already scripting, give it a shot.

Jojo Yee 12. Dec 2009 02:41 AM

Looks good. Will try it out when I get time. :)

bk_7312 14. Dec 2009 06:23 AM

My head is spinning from checking the man pages of sed and regex, just when I barely understood how sed works, I forgot half of what I learned when I closed the man pages, there's so many -options and commands that it's too overwhelming for me at my current level of understanding. As for regex, I was completely thrown off by the symbols that were used in conjunction with regex, symbols such as ^.[$()|*+?{\, debtboy was right when he said regex is very powerful, but not so easy to explain.

The good news is that I can understand the script (well... sort of anyways).

All times are GMT +1. The time now is 07:44 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.