SPUG: Get the local IP address?

Bill Campbell bill at celestial.com
Wed Jul 20 16:43:12 PDT 2005


On Wed, Jul 20, 2005, Atom Powers wrote:
>I know. I was just hoping for a simpler solution.
>Regex is so easy to get wrong...

Here's the guts of my routine parsing Linux ifconfig.  The
regular expressions are rather trivial.

    open(IFCONFIG, "/sbin/ifconfig |");
    my $key = '';
    while(<IFCONFIG>) {
        chomp;
        # print STDERR;
        if(/^(\S+)/) {
            $key = $1;
            $rc{$key} = {};
        }
        $rc{$key}->{HWaddr} = $1 if(/HWaddr\s+(\S+)/);
        $rc{$key}->{ipaddr} = $1 if(/inet addr:(\S+)/);
        $rc{$key}->{bcast}  = $1 if(/Bcast:(\S+)/);
        $rc{$key}->{mask}   = $1 if(/Mask:(\S+)/);
    }

Bill
--
INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Software LLC
UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

``The only freedom which deserves the name, is that of pursuing our own good
in our own way, so long as we do not attempt to deprive others of theirs,
or impede their efforts to obtain it.'' -- John Stuart Mill, 1859


More information about the spug-list mailing list