0

How could i get a list of all interfaces on a host with snmp, i am using.

  use Net::SNMP::Interfaces;

    my $interfaces = Net::SNMP::Interfaces->new(Hostname => 'localhost',
                                                Community => 'public' );

    my @ifnames = $interfaces->all_interfaces();

But i am getting the reply of:

root@localhost:~# perl i.pl
Can't call method "all_interfaces" on an undefined value at i.pl line 6.
jogn Loew
  • 1
  • 1
  • How was `Net::SNMP::Interfaces` installed? Did you have to do a "forced" install? Have you tried adding `use strict;`? – Red Cricket Jan 24 '14 at 07:01
  • Even with strict, it does not work. and yes i have Net::SNMP::Interfaces installed. – jogn Loew Jan 24 '14 at 08:02
  • I asked how was Net::SNMP::Interfaces? Maybe it was installed via `perl -MCPAN -e "force install Net::SNMP::Interfaces"` for some reason. – Red Cricket Jan 24 '14 at 08:14
  • Another thought. Can you do an `snmpwalk` on the localhost? Maybe the `->new` is failing because `snmpd` is not running or using some other community string. – Red Cricket Jan 24 '14 at 08:17
  • snmpwalk is working fine, and no i did not force install it. – jogn Loew Jan 24 '14 at 08:32
  • what do you get if you try adding `eval` and `die` (http://perldoc.perl.org/functions/die.html) error handling. – Red Cricket Jan 24 '14 at 08:44

1 Answers1

1

I think Red Cricket has the right idea. Net::SNMP::Interfaces->new will return undef if something went wrong.

Can you try executing the following?

#!/usr/bin/perl
use strict;
use warnings;
use Net::SNMP::Interfaces;

use Data::Dumper;

my $interfaces = Net::SNMP::Interfaces->new(
  Hostname => 'localhost',
  Community => 'public'
) or die "Error: $Net::SNMP::Interfaces::error";
my @ifnames = $interfaces->all_interfaces();

print Dumper \@ifnames;
Pierre.Vriens
  • 1,159
  • 34
  • 15
  • 19
Gordolio
  • 376
  • 3
  • 5