Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found