Greetings, Here is my code:
#!/usr/bin/perl -w use strict; use Net::SNMP; # this script gets passed a machine name ($machine_name) # and then an unknown number of commands if(!defined($ARGV[0])) { print "this scripts takes arguments example: test.pl machine.name comm +and1 command2 command3"; exit(1); } my $machine_name = $ARGV[0]; my $community = 'public'; my $snmp_port = 161; my @Commands; my $i; for ($i=1; $i < $#ARGV; $i++) { push @Commands, $ARGV[$i]; } #test to make sure variables have values. print "/nMachine:$machine_name"; foreach my $command_test (@Commands) { print "command: $command_test\n"; } print "Building Session..."; my ($session, $error) = Net::SNMP->session( -hostname => $machine_name, -community => $community, -port => $snmp_port ); ################################# ### This code above used to be... ### my ($session, $error) = Net::SNMP->session( ### -hostname => shift || 'localhost', ### -community => shift || 'public', ### -port => shift || 161 ### ); ################################# if (!defined($session)) { printf("ERROR in session: %s.\n",$error); exit 1; } ### general OID for system up time my $sysUpTime = '1.3.6.1.2.1.1.3.0'; ### These are Cisco OID's which i use for the AS5400 bank my $ModemsAvailable = '1.3.6.1.4.1.9.9.47.1.1.7.0'; my $ModemsUnavailable = '1.3.6.1.4.1.9.9.47.1.1.8.0'; my $result = $session->get_request( -varbindlist => [$sysUpTime] ); if(!defined($result)) { print "error in result: %s, status:%s.\n",$session->error,$session->er +ror_status); $session->close; exit 1; } my $result2 = $session->get_request( -varbindlist => [$ModemsAvailable] ); if(!defined($result2)) { print "error in result2: %s, status:%s.\n",$session->error,$session->e +rror_status); $session->close; exit 1; } my $result3 = $session->get_request( -varbindlist => [$ModemsUnavailable] }; if(!defined($result3)) { print "error in result3: %s, status:%s.\n",$session->error,$session->e +rror_status); $session->close; exit 1; } print "\nSystem uptime for host: '%s' is %s\n",$session->hostname, $re +sult->{$sysUpTime} ); print "\nModems Available for host: '%s' is %s\n",$session->hostname, +$result->{$sysUpTime} ); print "\nModems Unavailable for host: '%s' is %s\n",$session->hostname +, $result->{$sysUpTime} ); exit 0;

Does anyone know of a way to loop through multiple requests without needing to make a new variable everytime?
I am planning on having hundreds of commands available, where you just pass a number as argument like 1 3, and that would mean return the system uptime, and the modems unavailable. I was thinking somthing like this:

%cmd_table = ( 1 => 'sysUpTime', 2 => 'ModemsAvailable', 3 => 'ModemsUnavailable' ) my @results; foreach my $command (@Commands) { if(!defined(my $result_temp = $session->get_request( -varbindlist => ['$'.%cmd_table{$command}] ))) { print "error with command #:%s, %s", $command, %cmd_table{$command}); push @results, $result_temp; } }
Do you think this would work better, excuse any
obvious syntax problems.
If you have any idea's on how to make this whole
thing better your input would be appriciated.
I Typed this code from reading off my xterm so
I havn't actually ran this and tested this code,
it might have little typo's in it that I missed
in the trasfer
Thanks,
Enigmae

In reply to Making a scalable Net::SNMP script? by enigmae

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.