[vox-tech] Perl question: determining the computer's IP address

Richard Crawford vox-tech@lists.lugod.org
Thu, 26 Jun 2003 12:03:11 -0700 (PDT)


Peter Jay Salzman said:

> for machines with one IP address (disregarding lo), just use the output
> of ifconfig with a little regex handling:
>
> p@satan$ ifconfig
> eth0      Link encap:Ethernet  HWaddr 00:C0:F0:48:DF:5A
>           inet addr:192.168.0.2  Bcast:192.168.0.255  Mask:255.255.255.0
> ^^^^^^^^^^^^^^^^^^^^^
>           you want this.

Perfect.  This is what I used:

my $IP = '';
my @lines = qx|/usr/sbin/ifconfig hme0| or die "Could not get execute
ifconfig: $!\n";
foreach (@lines) {
  if (/inet ([\d.]+)/) {
    $IP = $1;
  }
}

Seems to work like a charm.  Thanks, Pete.

Sliante,
Richard S. Crawford

http://www.mossroot.com
AIM: Buffalo2K ICQ: 11646404 Y!: rscrawford
MSN: underpope@hotmail.com

"It is only with the heart that we see rightly; what is essential is
invisible to the eye." --Antoine de Saint Exupéry