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...]
Mac OS X Hints
http://hints.macworld.com/article.php?story=20051018043334366