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.....


In reply to Looping through numerous snmp strings.... by timmytimay

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.