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

after staring at this screen for about 20 minutes I finally gave up and have posted this here, i need a reg-ex that will pull the ip out of an ifconfig -a of the local machiene.

but i cannot figure out how to make it work on both types of OS's that we use here as Sun does it their way and Linux does it differently. so here are the two things that i need to match. i need the inet adder of eth0 and hme0,

thanks for helping with this simple question.
eth0 Link encap:Ethernet HWaddr 00:40:F4:48:1F:B1 inet addr:192.168.0.100 Bcast:192.168.0.255 Mask:255.255.2 +55.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:163706 errors:0 dropped:0 overruns:0 frame:0 TX packets:140415 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:24892923 (23.7 Mb) TX bytes:35772845 (34.1 Mb) Interrupt:12 Base address:0x6000 lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:1057866 errors:0 dropped:0 overruns:0 frame:0 TX packets:1057866 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:88084418 (84.0 Mb) TX bytes:88084418 (84.0 Mb) ........ lo0: flags=1000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4> mtu 8232 index +1 inet 127.0.0.1 netmask ff000000 hme0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 inde +x 2 inet 192.168.132.10 netmask ffffff00 broadcast 192.168.132.255
thanks for the help

jcpunk

by the way thanks for all the help that was, is, and will be

Replies are listed 'Best First'.
Re: patern matching difficulties, in two cases
by Tomte (Priest) on Jun 24, 2003 at 16:51 UTC

    I suggest using Net::Ifconfig::Wrapper.pm

    regards,
    tomte


    Hlade's Law:

    If you have a difficult task, give it to a lazy person --
    they will find an easier way to do it.

      thanks i will definatly look into that and see if i can get it to work (sometimes CPAN gives me the finger and sometimes my lack of perl skill also screws me up rather frequently)
      jcpunk

      by the way thanks for all the help that was, is, and will be

Re: patern matching difficulties, in two cases
by pzbagel (Chaplain) on Jun 24, 2003 at 16:58 UTC

    It would be a lot more accurate to write two different expressions, one for each OS and then just have your script check which OS it is running on and use the appropriate regex.

    $sunifconfig=qr/blahblahblah/; $linuxifconfig=qr/blahblahblah/; if ($^O eq "linux"){ $ifconfigoutput=~/$linuxifconfig/; } else { $ifconfigoutput=~/$sunifconfig/; }

    *That code is untested.

    Fellow monks, I don't know if using the $^O ($OSNAME) variable is the right way to go here. Feel free to comment.

    HTH

      Make sure you specify the full path to ifconfig as well, you will not that solaris has a few versions of it and they have different outputs.

      /sbin/ifconfig interface [ address_family ] [ address [ dest_address ] ] [ up ] [ down ] [ auto-revarp ] [ net- mask mask ] [ broadcast address ] [ metric n ] [ mtu n ] [trailers | -trailers ] [private | -private ] [arp | -arp ] [ plumb ] [ unplumb ] /usr/sbin/ifconfig interface [ address_family ] [ address [ dest_address ] ] [ up ] [ down ] [ auto-revarp ] [ netmask mask ] [ broadcast address ] [ metric n ] [ mtu n ] [trailers | -trailers ] [private | -private ] [arp | -arp ] [ plumb ] [ unplumb ] /sbin/ifconfig interface {auto-dhcp | dhcp } [ primary ] [ wait seconds ] drop | extend | ping | release | start | status /usr/sbin/ifconfig interface {auto-dhcp | dhcp } [ pri- mary ] [ wait seconds ] drop | extend | ping | release | start | status


      -Waswas
        thank you very much for that information, it is most helpful
        jcpunk

        by the way thanks for all the help that was, is, and will be

      thank you for that idea, i did not know that the $^O existed, and i will definatly look into that
      jcpunk

      by the way thanks for all the help that was, is, and will be

Re: patern matching difficulties, in two cases
by sgifford (Prior) on Jun 24, 2003 at 16:55 UTC
    This works for me:
    #!/usr/bin/perl while (<>) { if (/\s*inet (addr:)?([\d\.]+) /) { print "$2\n"; } }
      That works beautifully on the linux box, but grabs the loopback on the sun system.... however, i thank the much for that as it makes life simpler and jumpstarts my brain
      jcpunk

      by the way thanks for all the help that was, is, and will be

        Changing the print to:
        print "$2\n" unless ($2 =~ /^127/);
        is the quick-n-dirty way to fix that.