Last active
October 9, 2015 07:28
-
-
Save 0xack13/3461931 to your computer and use it in GitHub Desktop.
Perl SNMP Sample
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /usr/local/bin/perl | |
| ### To install Perl SNMP Module: perl -MCPAN -e shell ; "install SNMP" | |
| use strict; | |
| use warnings; | |
| use Net::SNMP; | |
| my $OID_sysUpTime = '1.3.6.1.2.1.1.3.0'; | |
| my ($session, $error) = Net::SNMP->session( | |
| -hostname => shift || 'localhost', | |
| -community => shift || 'public', | |
| ); | |
| if (!defined $session) { | |
| printf "ERROR: %s.\n", $error; | |
| exit 1; | |
| } | |
| my $result = $session->get_request(-varbindlist => [ $OID_sysUpTime ],); | |
| if (!defined $result) { | |
| printf "ERROR: %s.\n", $session->error(); | |
| $session->close(); | |
| exit 1; | |
| } | |
| printf "The sysUpTime for host '%s' is %s.\n", | |
| $session->hostname(), $result->{$OID_sysUpTime}; | |
| $session->close(); | |
| exit 0; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| use NetSNMP::agent; | |
| my $agent; | |
| sub myhandler { | |
| my ($handler, $registration_info, $request_info, $requests) = @_; | |
| my $request; | |
| $request->setValue(ASN_OCTET_STRING, "hello\n"); | |
| } | |
| $agent = new NetSNMP::agent( | |
| 'Name' => 'my_agent_name', | |
| ); | |
| $agent->register( | |
| "my_agent_name", | |
| ".1.3.6.1.4.1.8072.9999.9999.9999", | |
| \&myhandler | |
| ); | |
| $agent->main_loop(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment