George Geller wrote:
# 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);

I think die() is considered more indicative than exit(1), but it's probably a tossup.

I wouldn't use that regex. It is both too complicated and too simple at the same time. It matches the first line with a dotted quad from 0 to 999. That works, but it doesn't do enough argument checking to be usable if you need to break the quad apart. And it does too much checking if you don't need to break the quad apart.

I would personally match on the "inet addr:" portion instead (I believe jhriv already showed how to write that regex). If I was really feeling pedantic, I would attempt to match on eth0 first and then match on "inet addr:"

-a

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

Reply via email to