On Mon, 31 Mar 2003, Bob Miller wrote:
>sub getmyipaddress() {
>
>    # returns the first non-loopback IP address printed by ifconfig
>
>    open IFC, "/sbin/ifconfig |" or die;
>    while (<IFC>) {
>       if (/inet addr:([-.0-9]+)/ and not $1 =~ /^127\./) {
>           return $1;
>       }
>    }
>    close IFC;
>}

Oops...file descriptor leak!  This fails to close the pipe unless there
were no non-loopback interfaces.  In this case it's harmless, but it's
usually a good idea to avoid such things.

sub getmyipaddress() {

    # returns the first non-loopback IP address printed by ifconfig

    my($result) = "127.0.0.1";
    open IFC, "/sbin/ifconfig |" or die;
    while (<IFC>) {
        if (/inet addr:([-.0-9]+)/ and not $1 =~ /^127\./) {
            $result = $1 if $result eq "127.0.0.1";
        }
    }
    close IFC;
    return $result;
}

This returns the first non-loopback address it finds, or "127.0.0.1" if
the loopback interface is the only interface.

               - Neil Parker

_______________________________________________
Eug-LUG mailing list
[EMAIL PROTECTED]
http://mailman.efn.org/cgi-bin/listinfo/eug-lug

Reply via email to