LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-15-2005, 10:38 AM   #1
Moses420ca
Member
 
Registered: Jul 2003
Distribution: Ubuntu
Posts: 142

Rep: Reputation: 15
How can I get my external IP address from behind a NAT?


Hi.
I behind a NAT enabled router and I need to find my external IP address for logging purposes. What I want is a command line tool that looks like this.

Code:
getip >> iplog.log
Thanks for any help. Drew
 
Old 06-15-2005, 11:33 AM   #2
Kdr Kane
Member
 
Registered: Jan 2005
Distribution: SUSE, LFS
Posts: 357

Rep: Reputation: 30
You can't get it directly from the machine you are on. You'll need to get a machine on the internet to tell you.

Here's one:
http://www.whatismyip.com/
 
Old 06-15-2005, 11:49 AM   #3
demian
Member
 
Registered: Apr 2001
Location: Bremen, Germany
Distribution: Debian
Posts: 303

Rep: Reputation: 30
Depending on the type of router you have this might not be possible. If your router speaks snmp you can use that to grep the WAN IP address of the router. If it doesn't I wouldn't know how to do it.

When I do

snmpwalk -v2c -c public myrouter ip (myrouter is the DNS name of the router and ip is not an ip address but the literal string "ip")

my router tells me anything ip related. The WAN ip is under ipAdEntAddr.
 
Old 06-15-2005, 12:56 PM   #4
Moses420ca
Member
 
Registered: Jul 2003
Distribution: Ubuntu
Posts: 142

Original Poster
Rep: Reputation: 15
Thank you. I don't have that command and before I find it I'll ask this. I have a command that works but it outputs more then just the IP address. I tried a few things but all in all, I ended up in perl.
Code:
#!/usr/bin/perl

my $external_ip;

my $external_ip = `lynx -dump "http://checkip.dyndns.org"`;
my $external_ip =~ s/Current IP Address: //;

print("$external_ip\n");
All I want is the IP address but it doesn't output anything. Maybe my regex skills arn't what they use to be.
 
Old 06-15-2005, 01:55 PM   #5
Looking_Lost
Senior Member
 
Registered: Apr 2003
Location: Eire
Distribution: Slackware 12.0, OpenSuse 10.3
Posts: 1,120

Rep: Reputation: 45
This works for me:

html2text http://checkip.dyndns.org | cut -c21-35
 
Old 06-15-2005, 03:06 PM   #6
Moses420ca
Member
 
Registered: Jul 2003
Distribution: Ubuntu
Posts: 142

Original Poster
Rep: Reputation: 15
I still don't understand why this regex won't work... Thanks. That's a nice command there. Although mine has a carriage-return before and after the ip address that I can't seem to get rid of. It looks like this;
Code:
123.123.123.123
 
Old 10-23-2008, 12:07 PM   #7
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 50
could someone make a package tar.gz for a new sourceforge one:

getip ??

based on getip.com??
 
Old 11-23-2009, 03:18 AM   #8
ngibsonau
LQ Newbie
 
Registered: Nov 2009
Posts: 1

Rep: Reputation: 0
Here is a solution I made to solve Moses420ca's problem using SNMP.
For my router I log in to routers web admin interface and
Enable SNMP Agent
Enable SNMP Traps

Then run this script filename "getip"
passing in the ip or DNS name of the router
If you don't pass any parameters it looks up the default gateway

Code:
#!/bin/bash

# output external IP address from behind NAT using SNMP on router


if [ "$1" != "" ]; then
  ROUTER="$1"
else
  ROUTER=$(route | grep default | awk '{ print $2 }')
fi

# echo Router: $ROUTER

ROUTERIP=$(nslookup mygateway1.ar7 | tail -2 | grep Address | awk '{ print $2}')

snmpwalk -v 2c -c public $ROUTERIP ip | grep ipAdEntAddr |grep -v $ROUTERIP |grep -v 127.0.0.1 | awk '{ print $4 }'
 
Old 10-28-2010, 10:54 AM   #9
rimote media
LQ Newbie
 
Registered: Oct 2010
Posts: 3

Rep: Reputation: 1
other way to do it

We have our own webservers and did not want to use a third party like dyndns or use snmp. This is our solution:

1. Create a php file in your webhosting account:

getip.php

2. put this code in it:

<?php echo $_SERVER["REMOTE_ADDR"]; ?>

3. Now you have 2 options to use it:

A. go to the URL with a browser and you will see your IP-adres.
B. use html2text or like to get the output in your Linux system.

We use Debian and html2text does not have the right capabilities so we use this:

# apt-get install curl

# curl -s http://yourdomainname/getip.php | html2text

Now you get a nice output that works on any Linux PC without the hassle of snmp or use third party tools. All under your control.

Hope this helps somebody...
 
1 members found this post helpful.
Old 10-28-2010, 10:51 PM   #10
estabroo
Senior Member
 
Registered: Jun 2008
Distribution: debian, ubuntu, sidux
Posts: 1,092
Blog Entries: 2

Rep: Reputation: 109Reputation: 109
Quote:
Originally Posted by Moses420ca View Post
Code:
#!/usr/bin/perl

my $external_ip;

my $external_ip = `lynx -dump "http://checkip.dyndns.org"`;
my $external_ip =~ s/Current IP Address: //;

print("$external_ip\n");
Change that s/// line to
$external_ip =~ s/.*?([\d\.]+).*/$1/;

Last edited by estabroo; 10-28-2010 at 10:52 PM. Reason: fixed quote
 
Old 03-13-2011, 06:18 AM   #11
sergani
LQ Newbie
 
Registered: Jan 2010
Distribution: Fedora, CentOS, OpenSuse, Oracle Enterprise Linux, MacOSX
Posts: 27

Rep: Reputation: 0
I know it's a very old thread, but you never know
I've written this very simple script for the above request:

Code:
#!/bin/bash
#
# Output public IP from behind NAT using checkip.dyndns.org
#
printf "Your Current IP address is: "
curl -s http://checkip.dyndns.org/ | cut -d ' ' -f 6 | sed s/"body\|html\|<\|>\|\/"//g
#
# Respect for opensource... Respect for sharing!!
# Script written by Sergani ... msergani@gmail.com
The output should be something like this:

Code:
Your Current IP address is: XYZ.XYZ.XYZ.XYZ
If you do not want the text at the beginning of your line, for scripting purposes for example, you may just remove the "printf" line

You may then place this script file (name it getip for example) in your /bin/ directory, change it's permission for all users (or the root only, as you like) to be able to execute it:

Code:
For all users:
chmod a+x /bin/getip
For root only:
chmod ug+x /bin/getip
That's all
I hope it helps, and I would ask that if you were to use the script, to not forget including my bottom lines.. very much appreciated guys!!
 
Old 03-13-2011, 08:42 AM   #12
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
I went even further for a script I wrote and created the following simple function that queries a random ip site. All of the ones I use output a plain-text address, so there's no html to filter out. Some of them do unfortunately tack a newline onto the end, however, but it's easy enough to remove those as necessary.
Code:
get-current-ip() {

xc=1
until (( xc == 0 )) ; do

     case $(( RANDOM % 6 )) in

          0) ip=$(wget -t 2 -T 5 -q -O- http://showip.codebrainz.ca/) ;;
          1) ip=$(wget -t 2 -T 5 -q -O- http://www.whatismyip.com/automation/n09230945.asp) ;;
          2) ip=$(wget -t 2 -T 5 -q -O- http://www.showmyip.com/simple/) ;;
          3) ip=$(wget -t 2 -T 5 -q -O- http://cfaj.freeshell.org/ipaddr.cgi) ;;
          4) ip=$(wget -t 2 -T 5 -q -O- https://secure.informaction.com/ipecho/) ;;
          5) ip=$(wget -t 2 -T 5 -q -O- http://icanhazip.com/) ;;

     esac

     xc=$?

done

echo -n "${ip//[^0-9.]}"   #if you don't want a trailing newline
#echo "$ip"                #if you want or don't mind newlines
}

get-current-ip
You can of course add additional sites of your own, but be careful how you construct the command, as the loop uses wget's exit code to determine if it successfully retrieved the address. Do any text filtering outside the loop after it's finished.
 
Old 04-23-2011, 07:05 AM   #13
_jerry_
LQ Newbie
 
Registered: Apr 2011
Posts: 1

Rep: Reputation: 0
Thumbs up How to get the external IP from your router

This is one way that works for me:


wget -O - -q icanhazip.com


This site responds to your request by returning your IP

Make an executable file name it whatever you'd like and off you go..... ;-)
 
Old 08-02-2011, 10:37 AM   #14
redir
Member
 
Registered: May 2004
Location: Virginia USA
Distribution: Debian_Ubuntu_FreeBSD
Posts: 108

Rep: Reputation: 15
Quote:
Originally Posted by _jerry_ View Post
This is one way that works for me:


wget -O - -q icanhazip.com


This site responds to your request by returning your IP

Make an executable file name it whatever you'd like and off you go..... ;-)
Dude, you just made my day. Thanks
 
Old 08-04-2011, 01:41 AM   #15
ADX
Member
 
Registered: Jul 2011
Location: 127.0.0.1
Distribution: Slackware64-13.37
Posts: 47

Rep: Reputation: 0
How about just:

Code:
curl whatismyip.org
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
public ip address from behind nat mrtwice Linux - Networking 4 09-13-2012 09:24 AM
Problem Configuring Address Restricted Nat cipherscrux Linux - Networking 1 06-22-2006 01:52 AM
blocking mac address and NAT com90185 Linux - Security 6 03-07-2005 06:37 PM
How to detect IP address via NAT server jerrytw Linux - Networking 7 11-08-2004 04:42 AM
how to use my Ip address if having a NAT router poeta_boy Linux - Networking 5 04-17-2004 06:10 PM


All times are GMT -5. The time now is 08:30 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration