Skip to content

Instantly share code, notes, and snippets.

@alejandroscf
Created April 9, 2020 23:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alejandroscf/01bc0b47895484ffb10e3c5707342697 to your computer and use it in GitHub Desktop.
Save alejandroscf/01bc0b47895484ffb10e3c5707342697 to your computer and use it in GitHub Desktop.
Net::SNMP error_status test script
#! /usr/local/bin/perl
#########################
# Usage: snmp_test.pl [host] [community]
#########################
use strict;
use warnings;
use Net::SNMP;
my $OID_sysUpTime = '1.3.6.1.2.1.1.3.0';
my $OID_mikrotikTemp = '1.3.6.1.4.1.14988.1.1.3.10.0';
my $hostname = shift;
my $community = shift;
################
# Testing v1
################
printf "################\n";
printf "version v1\n";
printf "################\n\n";
my ($session, $error) = Net::SNMP->session(
-hostname => $hostname || 'localhost',
-community => $community || 'public',
# -translate => 0,
# -domain => 'udp',
-version => 'v1',
# -port => 161,
);
if (!defined $session) {
printf "ERROR: %s.\n", $error;
exit 1;
}
my $result2 = $session->get_request(-varbindlist => [ $OID_mikrotikTemp ],);
if (!defined $result2) {
printf "ERROR: %s. error_status=%s\n",$session->error(),$session->error_status();
} else {
printf "The mikrotikTemp for host '%s' is %s. error_status=%s\n",
$session->hostname(), $result2->{$OID_mikrotikTemp},$session->error_status();
}
my $result = $session->get_request(-varbindlist => [ $OID_sysUpTime ],);
if (!defined $result) {
printf "ERROR: %s. error_status=%s\n",$session->error(),$session->error_status();
} else {
printf "The sysUpTime for host '%s' is %s. error_status=%s\n",
$session->hostname(), $result->{$OID_sysUpTime},$session->error_status();
}
$session->close();
################
# Testing v2c
################
printf "\n################\n";
printf "version v2c\n";
printf "################\n\n";
my ($session2, $error2) = Net::SNMP->session(
-hostname => $hostname || 'localhost',
-community => $community || 'public',
# -translate => 0,
# -domain => 'udp',
-version => 'v2c',
# -port => 161,
);
if (!defined $session2) {
printf "ERROR: %s.\n", $error2;
exit 1;
}
my $result3 = $session2->get_request(-varbindlist => [ $OID_mikrotikTemp ],);
if (!defined $result3) {
printf "ERROR: %s. error_status=%s\n",$session2->error(),$session2->error_status();
} else {
printf "The mikrotikTemp for host '%s' is %s. error_status=%s\n",
$session2->hostname(), $result3->{$OID_mikrotikTemp},$session2->error_status();
}
my $result4 = $session2->get_request(-varbindlist => [ $OID_sysUpTime ],);
if (!defined $result4) {
printf "ERROR: %s. error_status=%s\n",$session2->error(),$session2->error_status();
} else {
printf "The sysUpTime for host '%s' is %s. error_status=%s\n",
$session2->hostname(), $result4->{$OID_sysUpTime},$session2->error_status();
}
$session2->close();
exit 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment