LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 05-29-2007, 01:56 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Rep: Reputation: 127Reputation: 127
How to know your external IP address?


How to know your external IP address?

(the external to the routeur or network one)
www.whatismyipaddress.com is one way but it is with firefox.

Thank you
 
Old 05-29-2007, 02:34 AM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
I assume you want a solution that gives you your external IP address from a commandline?

If so, this works - though it may not be the "best":
Code:
wget -q -O - http://www.whatismyipaddress.com   | \
grep "Your IP address is"                       | \
sed 's@.*address is \([^<]\+\).*@\1@'
Each command in the pipeline was put on a separate line to make it easier to see each step--purely aesthetic.

EDIT:
I do something like this for my home server. My router reports its external IP address on its status page. So, I've set up a script to check my router's status page for changes to the external IP address. The script sends an email to an external email account when a change occurs. Because I look at my own router, I don't have a problem with running the script every hour with cron; I'm not causing unnecessary 'net traffic nor am I pestering another person's server with repeated requests.

Last edited by Dark_Helmet; 05-29-2007 at 02:56 AM.
 
Old 05-29-2007, 03:02 AM   #3
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Original Poster
Rep: Reputation: 127Reputation: 127
Quote:
Originally Posted by Dark_Helmet
I assume you want a solution that gives you your external IP address from a commandline?

If so, this works - though it may not be the "best":
Code:
wget -q -O - http://www.whatismyipaddress.com   | \
grep "Your IP address is"                       | \
sed 's@.*address is \([^<]\+\).*@\1@'
Each command in the pipeline was put on a separate line to make it easier to see each step--purely aesthetic.

EDIT:
I do something like this for my home server. My router reports its external IP address on its status page. So, I've set up a script to check my router's status page for changes to the external IP address. The script sends an email to an external email account when a change occurs. Because I look at my own router, I don't have a problem with running the script every hour with cron; I'm not causing unnecessary 'net traffic nor am I pestering another person's server with repeated requests.
I noticed they changed the whatismyipaddress.com and use now some Gif of it.
is there a solution via commandline via i dont know maybe traceroute / iptable ... sthg
thank you

Have a good day
 
Old 05-29-2007, 04:24 AM   #4
Virtuality
Member
 
Registered: Jan 2007
Distribution: Debian Etch
Posts: 193

Rep: Reputation: 30
Quote:
It worked for me, you pasted one line in, enter, next, enter, next... then it gives you a white space, just hit enter again, and your IP comes up.

[quote[[root@localhost ~]# wget -q -O - http://www.whatismyipaddress.com | \
> grep "Your IP address is" | \
> sed 's@.*address is \([^<]\+\).*@\1@'

11.222.333.444
Maybe I don't understand what you're saying. Would whatismyip.com work any better for you?

Last edited by Virtuality; 05-29-2007 at 04:27 AM.
 
Old 05-29-2007, 02:52 PM   #5
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Original Poster
Rep: Reputation: 127Reputation: 127
[QUOTE=Virtuality]
Quote:
It worked for me, you pasted one line in, enter, next, enter, next... then it gives you a white space, just hit enter again, and your IP comes up.



Maybe I don't understand what you're saying. Would whatismyip.com work any better for you?
Thx
No my idea was :
Code:
 traceroute google.be
but it didnt work out...
hmm

If I recall well, with IPTABLES, once I was able to see my external ip address ... right or fully wrong ?... that was a long experiment with iptables ... cant recall well
 
Old 05-29-2007, 03:09 PM   #6
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Original Poster
Rep: Reputation: 127Reputation: 127
Quote:
Originally Posted by Dark_Helmet
I assume you want a solution that gives you your external IP address from a commandline?

If so, this works - though it may not be the "best":
Code:
wget -q -O - http://www.whatismyipaddress.com   | \
grep "Your IP address is"                       | \
sed 's@.*address is \([^<]\+\).*@\1@'
Each command in the pipeline was put on a separate line to make it easier to see each step--purely aesthetic.

EDIT:
I do something like this for my home server. My router reports its external IP address on its status page. So, I've set up a script to check my router's status page for changes to the external IP address. The script sends an email to an external email account when a change occurs. Because I look at my own router, I don't have a problem with running the script every hour with cron; I'm not causing unnecessary 'net traffic nor am I pestering another person's server with repeated requests.

Concerning this:
I am interested:
EDIT:
I do something like this for my home server. My router reports its external IP address on its status page. So, I've set up a script to check my router's status page for changes to the external IP address. The script sends an email to an external email account when a change occurs. Because I look at my own router, I don't have a problem with running the script every hour with cron; I'm not causing unnecessary 'net traffic nor am I pestering another person's server with repeated requests.

Dont you have a login and password to access your router ??
that a big not easy to access this router status information no ??

thank you
that s the greatest ideao to check the status router page ... ... pswd problem ... hmm
 
Old 05-29-2007, 03:34 PM   #7
stealth_banana
Member
 
Registered: Mar 2004
Location: Scotland
Distribution: Debian Sid / Kubuntu
Posts: 170

Rep: Reputation: 30
Does your router support SNMP (Simple Network Management Protocol)?

If so, it should be trivial to get your external IP. I use it for monitoring the SNR of my connection using gkrellm.
 
Old 05-29-2007, 04:08 PM   #8
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Original Poster
Rep: Reputation: 127Reputation: 127
Quote:
Originally Posted by stealth_banana
Does your router support SNMP (Simple Network Management Protocol)?

If so, it should be trivial to get your external IP. I use it for monitoring the SNR of my connection using gkrellm.

http://en.wikipedia.org/wiki/Simple_...ement_Protocol
pfff Trying to understand ...

Normally it should.

Sounds cool !
 
Old 05-29-2007, 05:17 PM   #9
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 370Reputation: 370Reputation: 370Reputation: 370
Code:
#!/bin/bash

# ======================================================================
# /home/user/cvsroot/src/utilities/external_ip/external_ip.bash,v
# ======================================================================
# Description:
#   This script sends an email to an external email service to notify
# users when this machine's external IP address changes. The IP address
# is read from the local router connected to the ISP. The router's
# administrator status page lists the external IP address.
#
# The general flow of the script is:
#   1. Retrieve the last known IP address (stored in a file)
#   2. Use wget to read the router's status page
#   3. Grep the router's status page for the IP address
#   4. Send an email to an external email address if the last IP
#      address differs from the current one.
#
# The script should run regularly--through cron or an equivalent.
# ======================================================================


# ======================================================================
# Variables to customize the script's operation
#
# Perhaps some of these should be supported by command line arguments
# ======================================================================

WGET=/usr/bin/wget
EGREP=/bin/egrep
ECHO=/bin/echo
RM=/bin/rm
MKDIR=/bin/mkdir
MUTT=/pkgs/inst/mutt/bin/mutt
MAC2UNIX=/pkgs/inst/mac2unix/bin/mac2unix
DATE=/bin/date
CAT=/bin/cat
DIRNAME=/usr/bin/dirname

# Customize these variables
ROUTER_IP_ADDRESS=???.???.???.???
PAGE_NAME=???
TEMP_DIR=???
USERNAME=???
PASSWORD=???
DATA_FILE=???
EMAIL_ADDRESS=???

# ======================================================================
# Start of the script.
#   1. Retrieve the last known IP address (stored in a file)
#
# If the data file does not exist, use a default value to guarantee a 
# mismatch with the new IP address
# ======================================================================
if [ -e ${DATA_FILE} ] ; then
  LAST_IP_ADDRESS=$( ${CAT} ${DATA_FILE} )
else
  LAST_IP_ADDRESS=xxx.xxx.xxx.xxx
fi


# ======================================================================
# Create a temporary directory to put files as they're created.
#
# Use a self-contained directory for easy cleanup at the end of the
# script.
#
# Assume this script is the only thing using the temp directory. Delete
# any prior copy of the temp directory and make a new one.
# -- The directory might exist if a previous run were interrupted or
#    killed during execution.
# ======================================================================
if [ -d "${TEMP_DIR}" ] ; then
  ${RM} -rf "${TEMP_DIR}"
fi

${MKDIR} -p "${TEMP_DIR}"

# ======================================================================
# Next major section of the script
#   2. Use wget to read the router's status page
#
# This is unique for my setup. To get the router's status page, my
# router requires an administrator login. The router denies access on
# the first attempt--don't know why. An immediate, subsequent attempt
# with the same credentials succeeds.
#
# To accomodate the router, the script uses a loop. It repeatedly
# requests the same page with the same credentials until the script
# reaches a maximum number of retries, or succeeds.
#
# Also, the router's statuts page is returned with Mac line endings.
# The line endings must be converted to Unix line endings or grep will
# not give good results.
# ======================================================================
wget_counter=0
wget_success=0
while [ \( ${wget_counter} -lt 10 \) -a \( ${wget_success} -ne 1 \) ] ; do
  ${WGET} --quiet                          \
          --http-user=${USERNAME}          \
          --http-passwd=${PASSWORD}        \
          --directory-prefix="${TEMP_DIR}" \
          "http://${ROUTER_IP_ADDRESS}/${PAGE_NAME}"
  wget_result=$?
  if [ ${wget_result} -ne 0 ] ; then
    let wget_counter=wget_counter+1
  else
    wget_success=1
  fi
done

# If wget_counter is equal to the maximum number of retries, then
# quit; we don't have a page to grep against.
if [ ${wget_counter} -eq 10 ] ; then
  exit 1
fi

${MAC2UNIX} "${TEMP_DIR}/${PAGE_NAME}"

# ======================================================================
# Next major section of the script
#   3. Grep the router's status page for the IP address
#
# At this point, the status page is available. Extract the new IP
# address from the page.
#
# My router returns a page with the external IP address is on a line
# of its own. In addition, the line with the external IP address is the
# only line that starts with a numeric digit.
# ======================================================================
new_ip_address=$( ${EGREP} "^[0-9]" "${TEMP_DIR}/${PAGE_NAME}" )

# ======================================================================
# Next major section of the script
#   4. Send an email to an external email address if the last IP
#      address differs from the current one.
#
# At this point, the current IP address is available. Compare it
# against the old IP address. If the IP addresses are different, send a
# notification email with the current IP address. If the IP addresses
# are the same, do nothing.
#
# Do not update the IP address stored in the data file if the command
# to email user(s) fails. This guarantees the stored IP address will
# differ from the current IP address the next time the script is run--
# causing another email attempt to notify the user(s).
# ======================================================================
if [ ${new_ip_address} != ${LAST_IP_ADDRESS} ] ; then
  ${ECHO} "New IP address detected at $( ${DATE} )" | \
  ${MUTT} -s "New Router IP address: ${new_ip_address}" ${EMAIL_ADDRESS}

  mail_success=$?
  if [ ${mail_success} -eq 0 ] ; then
    if [ -e ${DATA_FILE} ] ; then
      ${ECHO} "${new_ip_address}" > ${DATA_FILE}
    else
      ${ECHO} "Error: No data file to store new IP address in!" >&2
    fi
  fi
fi

# Cleanup the temporary workspace
${RM} -rf "${TEMP_DIR}"

exit 0
If you want to use this script, make sure you read the comments to fully understand what the script is doing, and know how to modify it for your needs.
 
Old 05-29-2007, 11:15 PM   #10
v00d00101
Member
 
Registered: Jun 2003
Location: UK
Distribution: Fedora 8, Centos 5.1
Posts: 480

Rep: Reputation: 30
If you have access to some external webspace that will run php, then put the following in a file and then grab it when you need the ip.

Code:
<?PHP
$ip = $_SERVER['REMOTE_ADDR'];
echo "Current IP Address: ", $ip;
?>
 
Old 05-29-2007, 11:28 PM   #11
wendea
Member
 
Registered: Mar 2007
Distribution: Fedora
Posts: 81

Rep: Reputation: 15
cant you run a traceroute and see where your IP goes? where it changes etc?
 
Old 07-07-2007, 06:43 AM   #12
Xeratul
Senior Member
 
Registered: Jun 2006
Location: Debian Land
Posts: 1,666

Original Poster
Rep: Reputation: 127Reputation: 127
Another solution:
________________

In 2 lines:

Code:
$ alias miip="wget http://checkip.dyndns.org/ -O - -o /dev/null | cut -d: -f 2 | cut -d\< -f 1"
$ miip
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I get my external IP address from behind a NAT? Moses420ca Linux - Networking 23 01-25-2013 06:35 AM
external ip address d1l2w3 Mandriva 68 01-13-2007 01:57 PM
Cannot see my webserver using the external IP address shazam75 Linux - Networking 12 09-30-2005 09:32 PM
SSH to an external IP address BluePyre Linux - Software 2 07-14-2004 07:10 PM
external ip address dica Linux - Networking 8 10-05-2003 10:57 PM


All times are GMT -5. The time now is 03:47 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration