timmytimay has asked for the wisdom of the Perl Monks concerning the following question:
Hi Monks, I’m a newbie/novice in need of a little help. I’m writing a network audit script to snmpget a few attributes from a list of devices. The problem I have is there are numerous snmp community strings in use, so if the snmpget fails on the first snmp community string it needs to try all till successful or just print ‘device must be dead’. I know it should be a something like a ‘foreach’ loop with ‘if’ conditions, but I just can’t nail the logic and the syntax. I would also like some advice on how I can read a file with IP subnets and the script to try all the host IPs in those subnets with the above snmpgets Cheers Timmytimay Here’s my code so far:
#!/usr/bin/perl # use warnings; use strict; my $workingdir = "/home/timmytimay/inventory_scripts"; my @snmpro=qw/snmpString1 snmpString2 snmpString3 snmpString4 snmpStri +ng5/; my $snmp_cmd = "/usr/sfw/bin/snmpget -v2c -c"; my $rtrlist = "$workingdir/RTR_LIST.TXT"; my $snmpget="/usr/sfw/bin/snmpget -v2c -c"; # # Open the device list open( RTR, "$rtrlist" ) || die "Can't open $rtrlist file"; # Open the log file and print the tiles open( LOG, ">$workingdir/RESULT.TXT" )|| die "Can't open $workingdir/R +ESULT.TXT file"; printf " Router\t\t Location\t\t\tContact\t\t Serial\n"; printf LOG " Router\t\t; Location\t\t\t;Contact\t\t ;Serial\n"; # Loop through the devices and... foreach (<RTR>) { chomp( $rtr = "$_" ); $rtr = `$snmpget .1.3.6.1.4.1.9.2.1.3.0 $snmpro[0] $rtr` or print "hos +tname failed: $!\n"; if ($rtr =~ m/Timeout:/){ $rtr = `$snmpget .1.3.6.1.4.1.9.2.1.3.0 $snmpro[1] $rtr` or print +"Host not responding to $snmpro[1]\n"; }
This is where I get stuck.....
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Looping through numerous snmp strings....
by grumbert (Scribe) on Sep 19, 2012 at 08:34 UTC | |
|
Re: Looping through numerous snmp strings....
by jwkrahn (Abbot) on Sep 19, 2012 at 19:51 UTC | |
by timmytimay (Initiate) on Sep 20, 2012 at 10:28 UTC |