inetipinfo – is a script, that will find/return your internet ip address on a linux machine

February 10, 2008

After searching for a program or a script for my linux distribution, that will give me my Internet ip address, i didn’t find

any. So I decided to write my own script, that has been tested and works. You can either copy the code from here, or a better

way is to download the package from the link below, and install it.


#!/bin/bash

### inetinfo
## inetinfo is a script, that will return your internet ip address on a linux machine.
## The script was written by Robert Mileski.
## You can contact the author for any suggestions or report bugs at the following email: r_mileski@hotmail.com

# Copyright (C) 2008
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

function message {
echo “Usage: `basename $0` [-n]”
echo “Outputs your internet IP address.”
echo “”
echo “Options:”
echo ” -n, –no-text : returns only the IP address without text”
echo ” -h, –help : show this usage message”
echo “”
echo “Mail bug reports and suggestions to Robert Mileski .”
}

function get_ip {
my_ip=`wget -q -O – http://www.myipaddress.com | grep -E ‘(^|[\>])[0-9]*\.[0-9]*\.[0-9]*\.[0-9]*([\<]|$)’ | sed

-e ‘s/<p>//’ -e ‘s/<b>//’ -e ‘s/<\/b>//’`
}

function exec_get_ip {
get_ip
if [ -z $my_ip ]; then
exit 1
else
echo $my_ip
fi
}

NO_ARGS=0
E_OPTERROR=65
wget_check=`which wget`

if [ $# -gt 1 ]; then
message
exit $E_OPTERROR
fi

if [ -z $wget_check ]; then
echo “You must have the wget package to use this script. Please first install wget.”
exit 1
fi

if [ $# -eq “$NO_ARGS” ]; then
get_ip
if [ -z $my_ip ]; then
echo “No connection to the Internet”
exit 1
else
echo “Your Internet IP address is: $my_ip”
exit 0
fi
fi

while getopts “nh-:” Option
do
case $Option in
h) message;;
n) exec_get_ip;;
-) case $OPTARG in
help) message;;
no-text) exec_get_ip;;
*) message;;
esac;;
*) message
exit $E_OPTERROR;;
esac
done

Don’t forget to make the file executable with chmod +x name

You can also download the whole installable script from this link: http://www.cscsoft.com/inetipinfo.tar.gz
Just extract the file, and run sudo make install

Best regards,
Robert Mileski