Gizmos Freeware Reviews  

Go Back   Gizmo's Freeware Forum > Freeware Forum > Linux

Reply
 
Thread Tools Display Modes
Old 12. Dec 2009, 01:27 AM   #1 (permalink)
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default 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:
Code:
#!/bin/bash

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

if [ "$address" == "" ]; then
     var_url="http://ipinfodb.com/ip_query.php"
else
     var_url="http://ipinfodb.com/ip_query.php?ip=$address"
fi

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...
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
debtboy is offline   Reply With Quote
Old 12. Dec 2009, 01:40 AM   #2 (permalink)
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default

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 location.sh, but you name it whatever you want.
In addition to saving the file, you will have to make it executable, use this command:
Code:
chmod 755 ./location.sh
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) techsupportalert.com. 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...
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
debtboy is offline   Reply With Quote
Old 12. Dec 2009, 01:57 AM   #3 (permalink)
Senior Member
 
debtboy's Avatar
 
Join Date: May 2009
Location: ~/
Posts: 128
Default

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 "techsupportalert.com" IP address, we found earlier
and the location (down to the nearest city) is given.


NOTE:
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!)

If your not already scripting, give it a shot.
__________________
[I][B]Linux, the choice of a GNU generation[/B][/I]
debtboy is offline   Reply With Quote
Old 12. Dec 2009, 02:41 AM   #4 (permalink)
Maestro di Search
 
Jojo Yee's Avatar
 
Join Date: Jul 2008
Posts: 7,809
Default

Looks good. Will try it out when I get time.
Jojo Yee is offline   Reply With Quote
Old 14. Dec 2009, 06:23 AM   #5 (permalink)
Senior Member
 
Join Date: Feb 2009
Location: 3rd largest island, smallest country there.
Posts: 229
Default

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).
bk_7312 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



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


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