我有一些我想要获得温度的设备。有些设备没有提供数字温度,只有OK或1.基本上我想在mib上进行轮询然后如果是空白轮询另一个,如果仍然是空白打印问题。
my $walk = `snmpwalk -O n -v 1 -c $community $switchaddr 1.3.6.1.4.1.9.9.13.1.3.1.3`;
$walk = substr("$walk", -3); #remove spacing
#print "\r$switchaddr $walk"
if ($walk =~ /^[0-9]+$/) { #If something is returned
print "$switchaddr $walk";
} elsif ($walk eq "") {
$walk = `snmpwalk -O n -v 1 -c $community $switchaddr 1.3.6.1.4.1.9.5.1.2.17`;
print "$switchaddr $walk\n"; #Should print 1
} else {
print "$switchaddr Problem\n";
}
感谢您的帮助。
答案 0 :(得分:3)
使用它,好多了:
#! /usr/local/bin/perl
use strict;
use warnings;
use Net::SNMP;
my $OID = '1.3.6.1.4.1.9.9.13.1.3.1.3';
my ($session, $error) = Net::SNMP->session(
-hostname => shift || 'localhost',
-community => shift || 'public',
);
if (!defined $session) {
printf "ERROR: %s.\n", $error;
exit 1;
}
my $result = $session->get_request(-varbindlist => [ $OID ],);
if (!defined $result) {
printf "ERROR: %s.\n", $session->error();
$session->close();
exit 1;
}
printf "The sysUpTime for host '%s' is %s.\n", $session->hostname(), $result->{$OID};
$session->close();