gnubbs has asked for the wisdom of the Perl Monks concerning the following question:

and regexs are already giving me sores. It has been a long time since I wrote anything in Perl, and my rusty mind is not being very cooperative. Looking at the Camel book I sweat that this is correct, but perl keeps yelling at me.

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

When run, I get the following error:
ROOT 113 tech_support_4 //devel# ./net_config.test
Use of uninitialized value at ./net_config.test line 8.
Use of uninitialized value at ./net_config.test line 8.

I am sure that this is an easy question, and I greatly appreciate your responses. Be gentle. It is 3:30 on a friday afternoon, happy hour at my favorite bar, and I am at work...

Replies are listed 'Best First'.
Re: Back in the saddle...
by Limbic~Region (Chancellor) on Apr 04, 2003 at 22:56 UTC
    gnubbs,
    Perhaps you want to try the following instead. It may be a little easier to use.

    #!/usr/bin/perl -w use strict; my $ip_address; open (NETSTAT,"/usr/bin/netstat -in |") or die "Unable to execute nets +tat"; while (<NETSTAT>) { next unless (/^ec0/); my @fields = split; $ip_address = $fields[3]; } print "$ip_address\n";

    I took the liberty of changing the ifconfig to a netstat because it is much easier to parse. This code can be greatly simplified, but since you say you are getting back in the saddle - I wanted to make it as clear as possible what I was doing.

    Cheers - L~R

      That was a great bit of advice. Didn't even think of using netstat. Something along these lines is probably what will end up in the script, but hopefully I will still find out what I was doing wrong in the original.

      thanks,
      gnubbs

Re: Back in the saddle...
by Mr. Muskrat (Canon) on Apr 04, 2003 at 23:00 UTC
    If all you want is the IP address...
    #!/usr/local/bin/perl -w use strict; my $ifconfig = `/usr/etc/ifconfig ec0`; my ($ip_address) = $ifconfig =~ /inet\s+(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d +{1,3})/; print "$ip_address\n";
Re: Back in the saddle...
by Nkuvu (Priest) on Apr 04, 2003 at 22:40 UTC

    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/;

      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".
Re: Back in the saddle...
by The Mad Hatter (Priest) on Apr 04, 2003 at 22:42 UTC
    It would seem as if `/usr/etc/ifconfig ec0` isn't returning any data and so $ifconfig is still undef. Try to run /usr/etc/ifconfig ec0 from the command line and see what the output is.

    Also, I believe the "use of undefined variable" error is actually just a warning and not fatal.

Re: Back in the saddle...
by jasonk (Parson) on Apr 05, 2003 at 23:59 UTC

    In the output of ifconfig the word 'inet' is always followed by a space, your regexp requires it to be followed by a word-character (\w), so the regexp never matches and the variables don't contain anything.


    We're not surrounded, we're in a target-rich environment!