get an IP address from behind a router

Captain Code

Moderator
Staff member
Mod
I don't want to post this in the other thread that's similar so I don't confuse everybody.

Currently I'm on a static IP, but I might be moving to ADSL soon which has a dynamic IP.

I have an internal IP address and want to update the dynamic DNS record with the external IP assigned to my hardware NAT router by my ISP.

I've pretty much figured out what I would need to do in theory but I lack the knowledge to implement it in a shell script or PERL script so I need any help you guys can give.

Here's what I'm thinking of doing:
-do a DNS lookup on the hostname my ISP assigns me
(usually in the form of xxxxxxx.city.region.isp.com or somethin)
-use that IP in one of the freely available PERL scripts to update the dynamic DNS record.

This doesn't sound too hard, but how would I go about doing a DNS lookup with either PERL or a shell script?
 
In the shell,
Code:
$ host myhostname
myhostname.domain.com has address 1.2.3.4

For perl, you could either call the above-mentioned host or
Code:
@ipaddrs = ( gethostbyname( "myhostname" ) )[ 4 ];
print "myhostname: ", join( unpack( 'C4', $ipaddrs[ 0 ] ), "." ), "\n";
 
Well, I don't know if you question is still pertinent to your situation or if it has been satisfactorily answered, but I'll go ahead and post this for the sake of the community.

I don't know of a way to get an external IP address without going outside of your router's domain. If anyone else does know how to get such information from within the confines of the LAN, I would love to know how.

Here's the code for a shell simple shell script from macosxhints.com (or alternately, it could be added as an alias to your .bashrc or .cshrc file):
Code:
#!/bin/sh
wget -q -O /dev/stdout [url]http://checkip.dyndns.org/[/url] | grep 'Current IP Address: ' | sed 's|<br>||g'
Note that it is a single line.

This queries an external web site that returns your ISP-assigned hostname and IP address (quietly), then pipes the output through grep and prints it to the shell.

If you prefer perl, here is a perl version:
Code:
[FONT=courier new]#!/usr/bin/perl
# prints current external IP address to shell

# URL of IP-checking script
$URL = 'http://checkip.dyndns.org/';
  
# string to grep
$gstr = 'Current IP Address:';

# string to split
$sstr = ': ';

# read html page
$line = `wget -q -O /dev/stdout $URL | grep '$gstr'`; 

# isolate IP address
chomp ($line); @ip = split ($sstr,$line);

# print to command line
print $ip[1]."\n";[/FONT]

I used the perl version of the code above to incorporate it into a script for updating my external IP address to my dynamic DNS service provider via the command line. They had a Linux perl script available, but it was non-functional on Mac OS X due to OS-specific portions of the code. The code above should be more portable.

The applications are virtually endless (have cron check and update your IP address at regular intervals, have cron mail you a message if your IP address changes, update manually from the command line with a simple command like checkip...)
 
If you just want your IP address, here's an easy one-liner:
wget -q -O /dev/stdout http://checkip.dyndns.org/ | grep 'Current IP Address: ' | awk ' { print $4 } '

You could then call it from a shell script like so:
IP=`wget -q -O /dev/stdout http://checkip.dyndns.org/ | grep 'Current IP Address: ' | awk ' { print $4 } '`
echo $IP
 
I still don't have DSL :mad:
Our phone company is very slow, and doesn't update their database with correct information.

Anyways, that looks like it would work great, thanks. Hopfully I'll be getting DSL soon, but I'm not counting on it.:rolleyes:
 
Back
Top