Weblog entry #1 for mnaumann
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
[ 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.
[ Parent | Reply to this comment ]
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 ]
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 ]
perl -MLWP::Simple -e 'get("http://checkip.dyndns.org") =~ /([\d.]+)/; print $1;'
[ Parent | Reply to this comment ]
wget -q -O - checkip.dyndns.org|sed -e&n bsp;'s/.*Current IP Address: //' -e 's/& lt;.*$//'
[ Parent | Reply to this comment ]
The even put your ip in the html title tag.
[ Parent | Reply to this comment ]
<?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 ]
Easy and simple...
wget www.whatismyip.org -qO -
No filtering needed.
[ Parent | Reply to this comment ]
--
Anurag
[ Parent | Reply to this comment ]
$ 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 ]