How can I get my external IP address from behind a NAT?
Linux - NetworkingThis forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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;
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 }'
#!/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
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!!
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.