Hi guys, I'm writing a simple script that uses snmpget statements to check the errors received by ports on a router and have the results going to a file. I've never worked with Perl before but one of my coworkers had a really basic script that I've been trying to rework into something better suited to this and I have a few questions regarding it.
1. Is there a way to make the IP addresses and OID's dynamic? Could I do an snmpwalk or something similar until it didn't find an OID? The reason I'm asking is because we're going to need to put this script on many client's machines and want little to no configuration required.
2. As of now, if the snmpget command times out trying to reach a router, you have to wait ~10 seconds before it tries the next OID it's given. Is there a way to check if the host is available before? Or if the IP isn't available, read back in the output from the console window somehow and have a simple if/else statement to skip that router?
I think that's it for now. Here's a copy of the code that I have so far. Like I said, I've never worked with Perl, but it's what he had a simple script written in and I tried to port it to C++ but had LOTS of problems with snmp libraries and could never get them to link correctly. Any help would be appreciated.
#!perl -w
use strict;
open LOG, ">>C:/log.txt";
my $now = localtime time;
print LOG "----------------------------------\n";
print LOG "$now\n----------------------------------\n";
my $SNMP_Get_Cmd = "snmpget -v1 -c wdpfRO -Ovq";
my $IP_Target;
my $SNMP_OID = ".1.3.6.1.2.1.2.2.1.14.";
my $Switch_Port = 0;
my $row = 0;
my $col = 0;
my $noRows = 3; # Number of Rows
my $noCols = 25; # Number of Columns
my $temp;
my @Error_Count = ( #IP ADDRESS, PORT: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
["192.168.3.1", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0","0", "0", "0", "0", "0", "0", "0", "0", "0"],
["192.168.3.2", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0","0", "0", "0", "0", "0", "0", "0", "0", "0"],
["192.168.3.17", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0", "0","0", "0", "0", "0", "0", "0", "0", "0", "0"]
);
# Retrieve Data From Switches
for($row = 0; $row<$noRows; $row++)
{
$IP_Target = $Error_Count[$row][0];
$temp = 'ping_check';
chomp($temp);
unless($temp=1)
{$row++;}
#If (output = "Timeout: No Response from " IP ".");
#$row++;
for($col = 1; $col<$noCols; $col++)
{ # The column is the port number
$temp = `${SNMP_Get_Cmd} ${IP_Target} ${SNMP_OID}${col}`;
chomp($temp);
$Error_Count[$row][$col] = $temp;
}
}
# Print A Table of all errors
print LOG "\n\t\t${Error_Count[0][0]}\t${Error_Count[1][0]}\t${Error_Count[2][0]}\n";
for($col = 1; $col<$noCols; $col++)
{
print LOG "Port ${col}: \t${Error_Count[0][$col]}\t\t${Error_Count[1][$col]}\t\t${Error_Count[2][$col]}\n";
}
print LOG "____________________________________________________________________\n\n\n";
close LOG;