Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A script to find a router's public non-static IP address UNIX
I was trying to run a web server from home using port forwarding to allow traffic through to the LAN machine, and the router has a non-static public IP address. I didn't want to keep logging into the admin page of the router, so I made this little script.

This script pings an external site once, and returns the routing information which is then chopped up by sed and awk to return the bare public IP address of the router, interspaced with periods. This version works in tcsh:
 ping -c 1 -R www.yahoo.com | awk -F: \
'{ if ($1 == "RR") print $2 }' | \
awk -F ( '{ print $2 }' | sed 's/)//' 
And here's a version that works in bash:
 ping -c 1 -R www.yahoo.com | awk -F: \
'{ if ($1 == "RR") print $2 }' | \
awk -F "(" '{ print $2 }' | sed 's/)//'
[robg adds: This worked as described...]
    •    
  • Currently 2.67 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (3 votes cast)
 
[19,495 views]  

A script to find a router's public non-static IP address | 19 comments | Create New Account
Click here to return to the 'A script to find a router's public non-static IP address' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A script to find a router's public non-static IP address
Authored by: jmdevaney on Oct 24, '05 06:09:21AM
This is what I use:


#!/bin/bash

curl -s http://checkip.dyndns.org | awk '{print $6}' | awk ' BEGIN { FS = "<" } { print $1 } '

---
Jdevaney

[ Reply to This | # ]

whatismyip.org
Authored by: guardian34 on Oct 24, '05 06:25:02AM

You can also use this:
curl -s http://whatismyip.org



[ Reply to This | # ]
whatismyip.org
Authored by: unforeseen:X11 on Oct 24, '05 08:37:22AM

Yes, this one is waaaay simpler. ;-)

---
this is not the sig you`re looking for.



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: statefuldotnet on Oct 24, '05 08:30:15AM
If you use the all powerful Quicksilver it comes with a Get External IP.scpt that can be quickly invoked and displays the IP in a huge Bezel display. I have internal IP mapped to "IP" and external mapped to EIP -- so either address is presented with only 4 (IP) or 5 (EIP) keystrokes from anywhere on the system. The "Get External IP.scpt" is:

set theIP to do shell script "curl -sf http://checkip.dyndns.org/|cut -d ':' -f 2|cut -d '

[ Reply to This | # ]

A script to find a router's public non-static IP address
Authored by: statefuldotnet on Oct 24, '05 08:32:09AM

set theIP to do shell script "curl -sf http://checkip.dyndns.org/|cut -d ':' -f 2|cut -d '<' -f1|sed -e 's/ //g'"
tell application "Quicksilver" to show large type theIP
return theIP

too even -- thanks html.



[ Reply to This | # ]
Check your router firmware for updates
Authored by: Anonymous on Oct 24, '05 09:03:24AM

D-Link and Linksys have both updated their routers to support automatic Dynamic DNS. I wasn't able to do this on my D-Link until I updated. This works perfectly with the free version of dyndns site.



[ Reply to This | # ]
Check your router firmware for updates
Authored by: nvdingo on Oct 24, '05 09:10:03AM

And even if you don't have a router that supports DynDNS built in, the DynDNS.org website has links to plenty of little scripts and apps that will parse your Public IP (routers) and auto update your dyndns.org account.

Did we mention that a dynamic dns account at it's most basic (which is what you need here) is free?



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: caesurae on Oct 24, '05 10:33:00AM

with the tcsh version, i get this result:

tcsh: Too many ('s.

using the bash version, i get this:

ping: record route: Invalid argument

i tried both versions with the appropriate shells.



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: burro on Oct 24, '05 10:34:27AM

or you can parse output from:

http://ipid.shat.net/



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: Code Masseur on Oct 24, '05 11:55:16AM

You aren't supposed to use automated scripts for http://ipid.shat.net/

Instead, their site directs automated scripts to use: http://ipid.shat.net/iponly/



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: Mike F. on Oct 24, '05 12:45:54PM

Doesn't work for me.

The problem is that the ping -c 1 -R www.yahoo.com statement doesn't list my external IP. So the rest of the script is useless. I'm using a Lancom DSL/i-10 Office Router with NAT on a dynamic IP DSL line.

To avoid dependencies on external resources I query the router itself via its web interface to get the current external IP. This has worked fine for years now.



[ Reply to This | # ]

A script to find a router's public non-static IP address
Authored by: gxw on Oct 24, '05 01:54:36PM

The sript in the hint doesn't work with a Netgear prosafe vpn firewall router either. Couple of the scripts in the comments work fine.

curl -s http://checkip.dyndns.org | awk '{print $6}' | awk ' BEGIN { FS = "<" } { print $1 } '

and

http://whatsmyip.com



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: peragrin on Oct 24, '05 01:50:12PM

If memory serves correctly this dashboard widget
http://www.apple.com/downloads/dashboard/networking_security/networkstat.html

does the same thing.

---
I thought once I was found but it was only a dream



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: nicola on Oct 25, '05 10:13:57AM
Just another useful site:
 curl "http://www.networksecuritytoolkit.org/nst/cgi-bin/ip.cgi" 
will give the IP address as text: no need to parse anything...

[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: sjk on Oct 25, '05 05:41:31PM
Ahh, that appends the missing newline I'd been adding with this script:

#!/bin/sh

printf "$(curl -s http://www.whatismyip.org)\n"


[ Reply to This | # ]
correction
Authored by: sjk on Oct 25, '05 05:45:05PM

Foo. Append a missing backslash before the final 'n' in that command line.



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: cagg on Oct 26, '05 05:59:18PM

Doesn't work for me either, using a Netgear wireless router to a cable connection. The RR line is actually another IP within my provider's network. The man page for ping says some routers ignore or discard the RECORD_ROUTE option so that may be a factor.

Interesting, though, is that traceroute DOES show my external ip as the first hop.



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: sjk on Oct 26, '05 10:51:49PM

Have you tried all the methods mentioned here?



[ Reply to This | # ]
A script to find a router's public non-static IP address
Authored by: gbilly on Jun 22, '06 08:12:18AM
Thanks for the hint. The "RR" line contained my local LAN IP not my external IP so I modified the script as follows:
ping -c 1 -R www.yahoo.com | \
sed -n '4p' | \
awk -F "(" '{print $2}' | \
sed 's/)//'

This is based on the 4th line ('4p') containing the usefull address. Can all of this be accomplished with sed?

Greg

[ Reply to This | # ]