2017-02-14 80 views
2

我正在嘗試對wlc上的AP狀態執行snmpwalk。我真的很陌生,因此對我裸露,但我正在與this guide。我能夠很好地獲得CPU利用率,但這只是一個獲取請求,因爲這是一個散步。不能使用未定義的值作爲HASH參考

我輸入:perl test.pl -H 10.192.54.30 -C public -O .1.3.6.1.4.1.14179.2.2.1.1.6.0 -w 20 -c 30

代碼:

#!/bin/perl 
use strict; 
use warnings; 
use Net::SNMP; 
use Getopt::Long qw(:config no_ignore_case); 


my $hostaddr = ''; 
my $community = ''; 
my $crit = ''; 
my $warn = ''; 
my $oid = ''; 

GetOptions(
     "host|H=s" => \$hostaddr, 
     "community|C=s" => \$community, 
     "crit|c:s" => \$crit, 
     "warn|w:s" => \$warn, 
     "oid|O=s" => \$oid); 

print "$hostaddr $community $crit $warn $oid\n"; 

my ($session, $error) = Net::SNMP->session(
         -hostname => "$hostaddr", 
         -community => "$community", 
         -timeout => "30", 
         -port => "161"); 

if (!defined($session)) { 
     printf("ERROR: %s.\n", $error); 
     exit 1; 
} 

my $response = $session->get_table(-baseoid => $oid); 

if (! defined $response) { 
    die "Failed to get OID '$oid': " . $session->error; 
} 

foreach my $key (keys %$response) { 
    print "$key: $response->{$key}\n"; 
} 

my $err = $session->error; 
if ($err){ 
     return 1; 
} 
print "\n"; 
exit 0; 

輸出:

10.192.54.30 public 30 20 .1.3.6.1.4.1.14179.2.2.1.1.6.0 
Can't use an undefined value as a HASH reference at test.pl line 26. 
+0

get_request()在你的代碼失敗。 $ response對象因此是未定義的。您嘗試訪問未定義的值。打印$ err以獲取更多信息。 – user3606329

+0

我在第29行添加了print $ err並獲得相同的輸出。 – cflinspach

+0

你確定嗎? $ session-> error()應該從get_request()打印出你的錯誤。請參閱:http://search.cpan.org/~dtown/Net-SNMP-v6.0.1/lib/Net/SNMP.pm#error()_-_get_the_current_error_message_from_the_object – user3606329

回答

2

幾個問題:

  • 你打電話$session->get_request錯誤。至少,您必須通過-varbindlist選項和一個OID陣列參考。請參閱documentation

  • get_request返回undef出錯,並且由於undef不是哈希引用,所以不能對其進行解引用。在嘗試使用$response之前,您必須檢查錯誤。

  • 您不應該將$response的內容複製到單獨的散列中以打印它們。

修正版本:

my $response = $session->get_request(-varbindlist => [$desc]); 

if (! defined $response) { 
    die "Failed to get OID '$desc': " . $session->error; 
} 

foreach my $key (keys %$response) { 
    print "$key: $response->{$key}\n"; 
} 

# Alternatively, 
# use Data::Dumper; print Dumper $response; 
+0

這似乎解決了一個問題,但現在我得到「在錯誤索引1處收到noSuchName(2)錯誤狀態」,但我可以使用snmpwalk輕鬆行走OID。 – cflinspach

+0

@red_eagle你可以顯示你運行完整的'snmpwalk'命令嗎? – ThisSuitIsBlackNot

+0

snmpwalk的-c公共10.192.54.30 -v 1 .1.3.6.1.4.1.14179.2.2.1.1.6 iso.3.6.1.4.1.14179.2.2.1.1.6.176.170.119.98.152.208 = INTEGER:1個 異。 3.6.1.4.1.14179.2.2.1.1.6.176.170.119.204.244.0 = INTEGER:1 iso.3.6.1.4.1.14179.2.2.1.1.6.244.207.226.84.120.224 = INTEGER:1 iso.3.6。 1.4.1.14179.2.2.1.1.6.244.207.226.133。180.16 = INTEGER:1 iso.3.6.1.4.1.14179.2.2.1.1.6.244.207.226.137.75.224 = INTEGER:1 iso.3.6.1.4.1.14179.2.2.1.1.6.244.207.226.137.116.80 = INTEGER:1 iso.3.6.1.4.1.14179.2.2.1.1.6.244.207.226.137.117.144 = INTEGER:1 – cflinspach