On Thu, 24 May 2001, David Matthew Monarres wrote:

> I wrote a program for my friend that will email him
> his ip address if it has change while he was go (I
> told him that this wasn't a great idea but he said it
> was the only way that he could access a home machine
> from work if his dsl dropped the connection) Right now
> I have a very rough version but it just seems ugly. He
> uses LINUX so basically I spawn an ifconfig and parse
> through the output. It would seem to me that since
> perl has a ton of networking libs that it would have
> one to get the ip of a given piece of hardware. I
> looked in the camel but didn't find anything that I
> thought that I could use. Was I wrong? would CPAN have
> something. Thank you in advance.

It's part of a standard Perl installation.  Type 'perldoc gethostbyname'
and you will get some documentation on various system calls to do things
like this.  Here's an example based on one from the Perl Cookbook (Recipe
17.8).

use Sys::Hostname;
use Socket;

my $hostname = hostname();

my ($name, $aliases, $addrtype, $length, @addresses) =
  gethostbyname($hostname) or die "Couldn't resolve hostname $hostname: $!";

my @names = ();
push(@names, $name);

foreach my $alias (split(/\s/, $aliases)) { push(@names, $alias) }
foreach my $addr (@addresses) { push(@names, join(".", unpack('C4', $addr))) }

@names should contain the hostname, any aliases and IP address of your
system.

-- Brett

Reply via email to