Perl 如果出现snmp问题
我有一些设备,我想从中获取温度。有些设备不提供数字温度,只有OK或1。基本上我想在mib上进行轮询,如果是空白轮询,则另一个轮询,如果仍然存在空白打印问题Perl 如果出现snmp问题,perl,snmp,Perl,Snmp,我有一些设备,我想从中获取温度。有些设备不提供数字温度,只有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 =
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";
}
谢谢你的帮助。使用这个,会更好:
#! /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();
使用此选项,效果会更好:
#! /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();
使用此选项,效果会更好:
#! /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();
使用此选项,效果会更好:
#! /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();
为什么不使用Net::SNMP?我强烈建议使用其中一种,这样您就可以使用单个会话进行多个查询,而不必手动解析结果(还有许多其他好处)。为什么不使用Net::SNMP?我强烈建议使用其中一种,这样您就可以使用单个会话进行多个查询,而不必手动解析结果(还有许多其他好处)。为什么不使用Net::SNMP?我强烈建议使用其中一种,这样您就可以使用单个会话进行多个查询,而不必手动解析结果(还有许多其他好处)。为什么不使用Net::SNMP?我强烈建议使用其中一种,这样您就可以使用单个会话进行多个查询,而不必手动解析结果(还有许多其他好处)。