This perl script works, but is not a idomatic perl.
How would a perl expert write it?
This is my second perl script that will actually be used by somebody else.

#!/usr/bin/perl -w

# George G. Geller
# IP-eth0.pl
# December 22, 2006

# Send the out the IP address of eth0 to stdout.

use strict;
my @ifcfg;
my $x;
my $ip;

@ifcfg = `/sbin/ifconfig eth0`;

# example:
# eth0      Link encap:Ethernet  HWaddr 00:14:2A:99:DF:2F  
#           inet addr:192.168.2.12  Bcast:192.168.2.255  Mask:255.255.255.0
#           inet6 addr: fe80::214:2aff:fe99:df2f/64 Scope:Link
#           UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
#           RX packets:43306 errors:0 dropped:0 overruns:0 frame:0
#           TX packets:64326 errors:0 dropped:0 overruns:0 carrier:0
#           collisions:0 txqueuelen:1000 
#           RX bytes:6378858 (6.0 MiB)  TX bytes:58482280 (55.7 MiB)
#           Interrupt:193 Base address:0xe000 


$x = $ifcfg[1];

chomp($x);

$_ = $x;
if (/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/) {
  $ip = $&;
  print "$ip\n";
} else {
  print "No match.\n";
  exit(1);
}

exit (0);



-- 
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to