Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Read network configuration on FreeBSD

by josef (Acolyte)
on Oct 10, 2012 at 17:00 UTC ( #998262=perlquestion: print w/ replies, xml ) Need Help??
josef has asked for the wisdom of the Perl Monks concerning the following question:

Hello Perlmonks,

I try to read the IP address, netmask and another parameters from the network card on the FreeBSD using IO::Interface and/or IO::Socket. On Linux the script run with no errors. My code:
#! /usr/bin/perl -w use IO::Socket; use Data::Dumper::Simple; use IO::Interface qw(:flags); my $s = IO::Socket::INET->new(Proto => 'udp'); @interfaces = $s->if_list; print Dumper @interfaces; for my $if (@interfaces) { next if $if =~ /usb|iwn|lo/; print "interface = $if\n"; print "addr = ",$if->addrese,"\n", "broadcast = ",$if->broadcast,"\n", "netmask = ",$if->netmask,"\n", "dstaddr = ",$if->dstaddr,"\n", "hwaddr = ",$if->hwaddr,"\n", "mtu = ",$if->mtu,"\n", "metric = ",$if->metric,"\n", "index = ",$if->index,"\n"; print "is running\n" if $if->is_running; print "is broadcast\n" if $if->is_broadcast; print "is p-to-p\n" if $if->is_pt2pt; print "is loopback\n" if $if->is_loopback; print "is promiscuous\n" if $if->is_promiscuous; print "is multicast\n" if $if->is_multicast; print "is notrailers\n" if $if->is_notrailers; print "is noarp\n" if $if->is_noarp; }
The result:
@interfaces = ( 'iwn0', 'lo0', 're0', 'usbus0', 'usbus1', 'usbus2', 'usbus3', 'usbus4', 'usbus5', 'usbus6', 'usbus7' ); interface = re0 Can't locate object method "addrese" via package "re0" (perhaps you fo +rgot to load "re0"?) at interfaces.pl line 25.
The line 25 is the line with print "addr = ",$if->addrese,"\n", ......

Any idea? Or another module? On the FreeBSD the ifconfig command output is like:

re0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1 +500 options=8209b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,WO +L_MAGIC,LINKSTATE> ether c8:0a:a9:93:87:25 inet 172.21.100.29 netmask 0xffffff00 broadcast 172.21.100.255 inet 192.168.1.5 netmask 0xffffffff broadcast 192.168.1.5 nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL> media: Ethernet autoselect (100baseTX <full-duplex>) status: active

Regards

Josef

Comment on Read network configuration on FreeBSD
Select or Download Code
Re: Read network configuration on FreeBSD
by VinsWorldcom (Priest) on Oct 10, 2012 at 17:09 UTC

    Seplling? That says 'addrese', I assume it should be 'address'?

    And if you're using the synopsis directly from the POD for IO::Interface, you probably shouldn't mix the new and old API.

      Ups, I correct the line with 'addrese' to 'address' but the error still remain :
      Can't locate object method "address" via package "re0" (perhaps you fo +rgot to load "re0"?) at interfaces.pl line 25.
      My script is now:
      #! /usr/bin/perl -w use Data::Dumper::Simple; use IO::Interface::Simple; my @interfaces = IO::Interface::Simple->interfaces; #print Dumper @interfaces; for my $if (@interfaces) { next if $if =~ /usb|iwn|lo/; print "interface = " . $if . "\n"; print "addr = " . $if->address . "\n"; print "broadcast = " . $if->broadcast ."\n"; print "netmask = " . $if->netmask ."\n"; print "dstaddr = " . $if->dstaddr . "\n"; # line 15 print "hwaddr = " . $if->hwaddr . "\n"; # line 16 print "mtu = " . $if->mtu . "\n"; print "metric = " . $if->metric . "\n"; print "index = " . $if->index . "\n"; print "is running\n" if $if->is_running; print "is broadcast\n" if $if->is_broadcast; print "is p-to-p\n" if $if->is_pt2pt; print "is loopback\n" if $if->is_loopback; print "is promiscuous\n" if $if->is_promiscuous; print "is multicast\n" if $if->is_multicast; print "is notrailers\n" if $if->is_notrailers; print "is noarp\n" if $if->is_noarp; } and the result is like: interface = re0 addr = 172.21.100.29 broadcast = 172.21.100.255 netmask = 255.255.255.0 Use of uninitialized value in concatenation (.) or string at int.pl li +ne 15 dstaddr = hwaddr = c8:0a:a9:93:87:25 mtu = 1500 metric = 0 index = 6 is running is broadcast is multicast Your vendor has not defined IO::Interface macro IFF_NOTRAILERS at /usr +/local/lib/perl5/site_perl/5.14.2/mach/IO/Interface/Simple.pm line 11 +7

        Just check if the values are defined before printing them. I don't have a linux system so I don't know why IO::Interface (which does't compile for me on Windows/Strawberry) doesn't return a dstaddr or hwaddr.

        printf "dstaddr = %s\n", defined($if->dstaddr) ? $if->dstaddr : ''; +# line 15 printf "hwaddr = %s\n", defined($if->hwaddr) ? $if->hwaddr : ''; # +line 16
        Hmm, the script read now the interface parameters but not for the IP addresses as alias.

        I will be very happy to get the media and the speed of the interface:

        media: Ethernet autoselect (100baseTX <full-duplex>)

Re: Read network configuration on FreeBSD
by aitap (Deacon) on Oct 10, 2012 at 17:38 UTC

    Actually, it doesn't work on Linux, too.

    Notice the difference: the interfaces you get are strings, the ones you need are IO::Interface::Simple objects:

    use Data::Dumper; use IO::Socket; use IO::Interface::Simple; print Dumper [ IO::Socket::INET::->new(Proto => 'udp')->if_list ]; print Dumper [ IO::Interface::Simple::->interfaces ]; print [IO::Interface::Simple::->interfaces]->[0]->address; __END__ $VAR1 = [ 'eth0', 'lo', 'teredo' ]; $VAR1 = [ bless( { 'name' => 'lo', 's' => bless( \*Symbol::GEN1, 'IO::Socket::INET' ) }, 'IO::Interface::Simple' ), bless( { 'name' => 'eth0', 's' => $VAR1->[0]{'s'} }, 'IO::Interface::Simple' ), bless( { 'name' => 'teredo', 's' => $VAR1->[0]{'s'} }, 'IO::Interface::Simple' ) ]; 127.0.0.1

    Sorry if my advice was wrong.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://998262]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2014-12-22 05:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (110 votes), past polls