in reply to Back in the saddle...

Shouldn't the ^ at the begining be outside of the parens?

What is the result of the ifconfig command? That is, have you tried printing out the value of $ifconfig to see if it really is what you think it is?

Missing a \ before the first d for the IP address, and a \ before the second dot, btw. Oh, and take off the parens around the match part of the statment, methinks.

Should be something like ($junk, $ip_address) = $ifconfig =~ /regex/;

Replies are listed 'Best First'.
Re: Re: Back in the saddle...
by gnubbs (Beadle) on Apr 04, 2003 at 22:51 UTC

    A slightly revised, but still broken script:

    #!/usr/local/bin/perl -w use strict; my $ifconfig = `/usr/etc/ifconfig ec0`; print "$ifconfig\n"; $_ = $ifconfig; my ($junk, $ip_address) = /^(.*\n.*inet\w)(\d{1,3}\.\d{1,3}\.\d{1,3 +})/; print "$junk $ip_address\n";

    Running this results in the following:
    ROOT 164 tech_support_4 //devel# ./net_config.test
    ec0: flags=410c43<UP,BROADCAST,RUNNING,FILTMULTI, MULTICAST,LINK0,IPALIAS>
    inet 172.22.43.157 netmask 0xfffffc00 broadcast 172.22.43.255
    Use of uninitialized value at ./net_config.test line 11.
    Use of uninitialized value at ./net_config.test line 11.

    (That ifconfig output is only on two lines on the system.)

      If this is the return when you run ifconfig:
      ec0: flags=410c43<UP,BROADCAST,RUNNING,FILTMULTI,MULTICAST,LINK0,IPALI +AS> inet 172.22.43.157 netmask 0xfffffc00 broadcast 172.22.43.255
      and this is your regex:
      /^(.*\n.*inet\w)(\d{1,3}\.\d{1,3}\.\d{1,3})/
      The problem is that you're trying to match and capture everything in the string up to and including the first IP address, when you don't need to. It's failing because of the part inet\w -- you're looking for an alphanumeric character immediately after the "t" of "inet", and there isn't one. The following would do what you want:
      my ($ip_address) = ( /inet\s+(\d{1,3}\.\d{1,3}\.d{1,3})/ );
      That is, match and capture the first three components of the IP address that follows one or more whitespace characters immediately after the first occurrence of "inet".