Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Looping through numerous snmp strings....

by timmytimay (Initiate)
on Sep 19, 2012 at 07:13 UTC ( #994413=perlquestion: print w/ replies, xml ) Need Help??
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.....

Comment on Looping through numerous snmp strings....
Download Code
Re: Looping through numerous snmp strings....
by grumbert (Scribe) on Sep 19, 2012 at 08:34 UTC
    You could do it something like this (just a rough draft though)
    foreach my $snmp_community (@snmpro) { if (`$snmpget $oid $snmpcommunity $rtr`) { process_snmpdata(); last; } else { say "Host not responding to $snmp_community"; next; } }
Re: Looping through numerous snmp strings....
by jwkrahn (Monsignor) on Sep 19, 2012 at 19:51 UTC
    open( RTR, "$rtrlist" ) || die "Can't open $rtrlist file"; ... open( LOG, ">$workingdir/RESULT.TXT" )|| die "Can't open $workingdir/R +ESULT.TXT file"; ... $rtr = `$snmpget .1.3.6.1.4.1.9.2.1.3.0 $snmpro[0] $rtr` or print "hos +tname failed: $!\n"; ... $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";

    Consistency is always good.    You should stick with either ( ... ) || ... or  ... or ...

    The use of $! with backticks is wrong.    You SHOULD be using $! to indicate errors from open and $? to indicate errors from backticks.

    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";

    You are using printf when you should be using print.

    foreach (<RTR>) { chomp( $rtr = "$_" );

    foreach needs to read the entire file into memory first before it can loop through the lines.    The usual, more efficient method is to use a while loop if you only need one line at a time.

    Why are you copying the value in $_ to a string when it is already a string?

      Thanks Monks - some great advice! I'll give it a go and let you know if I have any more queries.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994413]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (3)
As of 2014-07-13 00:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (244 votes), past polls