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

Hy all, I've searched a lot of archives and newsgroups, but they all told me what I allready know. $blah=`ìfconfig $device` So by using the backticks ... Isn't there any other native perl way of getting NIC device information ? Or if nobody has an answer to this, I'd also like to be rich easy and fast. Any comments on this are also much 'preciated :-) Cya, Djeezus

Replies are listed 'Best First'.
Re: ifconfig / ipconfig replacement
by converter (Priest) on Aug 22, 2001 at 05:26 UTC

    This may or may not meet your needs, but it looks useful:

    IO::Interface - Perl extension for access to network card configuration information

      If you are looking for an OS independent method of getting IP info then you might try what tadman suggested to me thanks tadman. The netstat -r is apparently OS independant. It needs to be processed to pick them out but that looks reasonably easy. To be honest I havn't had a chance to try it yet (I'm waiting for a *nix head to email me the output it produces)
Re: ifconfig / ipconfig replacement
by idnopheq (Chaplain) on Aug 22, 2001 at 14:28 UTC
    Check out Re: Checking my default gateway. It's something I whipped together for NT/2K. Also, someone on Activestate's Perl Win32 Admin mail list had a WMI version, prob. more portable within Win32 platforms.

    For *nix, there is usually a pastle of files you could read to get the information, for instance on Solaris some include:

    • /etc/hostname.ifno
    • /etc/hosts
    • /etc/netmasks
    • /etc/system
    • /etc/defaultdomain
    • /etc/defaultrouter
    • /etc/resolv.conf
    • /etc/nodename

    UPDATE: Here is the one from the AS list I mentioned. I cannot find the original post, so the author will remain anonymous. Please note that I take no credit for this.

Re: ifconfig / ipconfig replacement
by nehlwyn (Acolyte) on Aug 22, 2001 at 05:36 UTC

    Greetings fellow monk ,

    I think that your idea to try to do something with a native way to Perl is good , because it's a way to learn new things , and to make your chunk of code portable . But i learn that , in a tutorial at YAPC , sometimes it may be also a good idea to make something simple work simply and usable in a minute ...

    For instance i do a beuah bad :

    @badcode=`ifconfig`; if (grep(/ppp0/,@badcode)){print"Connexion PPP OK !!!}

    But it took me 30 secs to write and it works ! But i'm sure some other more cultured monk will give you a more elegant manner . And i , too , will gladly learn from it ...

    the little one.
Re: ifconfig / ipconfig replacement
by Roghue (Initiate) on Apr 30, 2004 at 01:01 UTC
    I've just recently taken up this project again, I dropped it back than and wrote the whole thing in sh. Now I'm adding network functionallity to it, so I must go back to perl. Here's how I got my list of interfaces/ip-addresses, as suggested previously, by using IO:Interface
    #!/usr/bin/perl -w # # Test script voor interface informatie # by Djeezus 30/04/2004 # ------------------------------------- use IO::Interface; use IO::Socket::INET ; # must create socket first, bound to all interfaces $sock = IO::Socket::INET->new(Proto => 'udp'); # get list of active interfaces @iflist = $sock->if_list; print "List interfaces : @iflist\n"; # get list of active interfaces/addresses for $intf (@iflist) { $addr = $sock->if_addr(${intf}); # creating hash interface/address $addrlist{"$intf"} = "$addr"; } # all active interface/ipaddress # using 'key' => 'value' from created hash while (($key,$value) = each %addrlist ) { print "$key\t=>\t$value\n" }