Learning Perl with SNMP Part I

Ok my first stab at it was great (snmp.pl)

#!/usr/bin/perl

$SNMP_GET_CMD = “snmpget -v1 -c public -Ovq”;
$SNMP_TARGET = “1.1.1.2″;

chomp($model = `${SNMP_GET_CMD} ${SNMP_TARGET} 1.3.6.1.2.1.1.3.0`);
chomp($serial = `${SNMP_GET_CMD} ${SNMP_TARGET} 1.3.6.1.2.1.1.4.0`);

$model =~ s/\”//g;      # Ditch the quotes.
$serial =~ s/\”//g;

print <<END;
APC UPS                         ${SNMP_TARGET}
Model: ${model}         Serial No: ${serial}
END

The output:

APC UPS                         1.1.1.2
Model: 33:17:35:52.07         Serial No: NOC 555-555-5555

Basically I’m using snmp-get from a command line in Perl

Script two I try to do more of a loop (snmp2.pl)

#! /usr/bin/perl
use strict;
use Net::SNMP;

my ($session, $error) = Net::SNMP->session(
-hostname  => shift || ’1.1.1.1′,
-community => shift || ‘public’,
-port      => shift || 161
);

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

my $sysUpTime = ’1.3.6.1.2.1.1.3.0′;
my $sysContact = ’1.3.6.1.2.1.1.4.0′;

my $result = $session->get_net(
-varbindlist => [$sysUpTime], [$sysContact]
);

if (!defined($result)) {
printf(“ERROR: %s.\n”, $session->error);
$session->close;
exit 1;
}

printf(“sysUpTime for host ‘%s’ is %s\n”,
$session->hostname, $result->${sysUpTime[0]}
);

$session->close;

exit 0;

The output:

sysInfo for host 1.1.1.1 is it-5-6509
sysInfo for host 1.1.1.1 is 254 days, 03:19:36.4

Now I’m looping through 2 OIDS and printing them line by line.

Part II will be putting them in an array.

You can leave a response, or trackback from your own site.

Leave a Reply

Login with Facebook:
Subscribe to RSS Feed Follow me on Twitter!