Perl – 使用Net :: SNMP :: Interfaces来列出主机的接口

我怎么能得到一个主机上的所有接口与snmp,我正在使用的列表。

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

但是我收到的答复是:

 root@localhost:~# perl i.pl Can't call method "all_interfaces" on an undefined value at i.pl line 6. 

我认为红蟋蟀有正确的想法。 Net::SNMP::Interfaces->new将返回undef,如果出错了。

你可以尝试执行以下?

 #!/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;