Weblog entry #1 for mnaumann

Determine your public IP address quickly
Posted by mnaumann on Fri 16 Mar 2007 at 11:55
Tags: none.

Here's what I consider a handy snippet to quickly determine your current external/public IP address. This can be useful in a NAT environment where you have no access to the firewall and networking configuration (such as in a virtual server which has no public IP address assigned to it).

GET checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'

If you don't have sed, try awk, or any scripting language which supports regular expressions (the pattern formatting may differ with other implementations).

GET requires perl to be installed. If you don't have perl, try one of these:

curl -s checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
links -dump checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'
lynx -dump checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'|grep -v '^$'

This snippet is dependant of the availability and current output formatting of http://checkip.dyndns.org/. Luckily, this URL is also used by many dyndns clients/updaters, so it's likely to remain the same for quite a while.

You know an easier way? Let me know!

 

Comments on this Entry

Posted by Steve (62.30.xx.xx) on Fri 16 Mar 2007 at 11:59
[ Send Message | View Steve's Scratchpad | View Weblogs ]

I've always used this:

#
#  Find our IP address
#
alias my_ip='wget -O - checkip.dyndns.org 2>/dev/null|cut -d ":" -f2|cut -d "<" -f1'

Not as neat as some of yours, but since it is a shell alias I can just type "my_ip" and get the result.

Steve

[ Parent | Reply to this comment ]

Posted by mnaumann (213.39.xx.xx) on Fri 16 Mar 2007 at 12:20
[ Send Message | View Weblogs ]

You're a quick commenter. Indeed, aliasing it is a nice idea. And I'm a perfectionist who dislikes leading spaces. So, here's an update based on your suggestion.

alias my_ip='wget -qO - checkip.dyndns.org|cut -d":" -f2|cut -d"<" -f1|cut -d" " -f2'

Based on timing tests, wget + cut is indeed much faster than perl's GET + sed (as one would expect). wget + cut should also use less memory and CPU resources.

[ Parent | Reply to this comment ]

Posted by ajt (204.193.xx.xx) on Fri 16 Mar 2007 at 12:54
[ Send Message | View Weblogs ]
If you are going to use a programming language like Perl/Python/Ruby then I'd do it all in that language and not bother with something like GET.

If you are going to do this in shell, then I like this alias, it's very nice and quick.

curl may or may not be faster than wget, the curl people think it's very quick.

--
"It's Not Magic, It's Work"
Adam

[ Parent | Reply to this comment ]

Posted by stu42j (129.250.xx.xx) on Fri 16 Mar 2007 at 14:20
[ Send Message ]
Good point. How about this?
perl -MLWP::Simple -e 'get("http://checkip.dyndns.org") =~ /([\d.]+)/; print $1;'

[ Parent | Reply to this comment ]

Posted by Anonymous (24.175.xx.xx) on Sun 31 Aug 2008 at 12:41
This is the fastest one-liner I can come up with aside from writing a C proggy to do it...
wget -q -O - checkip.dyndns.org|sed -e&n bsp;'s/.*Current IP Address: //' -e 's/& lt;.*$//'

[ Parent | Reply to this comment ]

Posted by mvanbaak (82.95.xx.xx) on Sat 17 Mar 2007 at 11:24
[ Send Message ]
Another source for this information is http://www.whatismyip.com
The even put your ip in the html title tag.

[ Parent | Reply to this comment ]

Posted by Anonymous (152.43.xx.xx) on Sat 17 Mar 2007 at 15:42
My solution is very simple: Set up a php page with the following:

<?php
header("Content-type: text/plain");
echo $_SERVER['REMOTE_ADDR'];
?>


Query the page with whatever; don't have to worry about parsing, etc.
I just keep this at [domain]/extip.php on a shared host and use it to get the IP for my home (DSL) connection.

[ Parent | Reply to this comment ]

Posted by rodaballo (213.73.xx.xx) on Sat 17 Mar 2007 at 23:13
[ Send Message ]

Easy and simple...

wget www.whatismyip.org -qO -

No filtering needed.

[ Parent | Reply to this comment ]

Posted by mnaumann (91.64.xx.xx) on Fri 6 Nov 2009 at 19:38
[ Send Message | View Weblogs ]
That's the best I've come across so far. Thanks.
Even shorter, if you have w3m:
w3m -dump whatismyip.org

[ Parent | Reply to this comment ]

Posted by anurag (59.181.xx.xx) on Thu 22 Mar 2007 at 11:45
[ Send Message ]
i have multiple isp connections at my place, and i have a habit of ssh-ing to my remote server and issuing "w" on shell to find out from which isp's gateway i am connected to :)

--
Anurag

[ Parent | Reply to this comment ]

Posted by Anonymous (64.142.xx.xx) on Wed 30 Jan 2008 at 20:50
using echo, nc, tail and tr:

$ echo -e "GET http://checkip.dyndns.org/ HTTP/1.0\r\n\r\n"|nc checkip.dyndns.org 80|tail -1|tr -dc '[.0-9\n]'

Also works from a reasonably complete busybox; add busybox to each command (I.E. 'busybox tr...') if you don't have symlinks.

Then again, if you have a complete busybox, this works too:

$ busybox wget -qO - http://checkip.dyndns.org |busybox tr -dc '[.0-9\n]'

[ Parent | Reply to this comment ]

User Login

Username:

Password:

[ Advanced Login ]

Register Account

Quick Site Search