summaryrefslogtreecommitdiff
path: root/cleopatre/application/spidnetsnmp/perl/SNMP/examples
diff options
context:
space:
mode:
Diffstat (limited to 'cleopatre/application/spidnetsnmp/perl/SNMP/examples')
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/async1.pl17
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/async2.pl19
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/bulkwalk.pl121
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/ipforward.pl30
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/mibtree.pl20
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/mibwalk.pl17
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/pingmib.pl36
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/tablewalk.pl19
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/testleak.pl19
-rw-r--r--cleopatre/application/spidnetsnmp/perl/SNMP/examples/trap-example.pl94
10 files changed, 392 insertions, 0 deletions
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/async1.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/async1.pl
new file mode 100644
index 0000000000..ffd6065ac9
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/async1.pl
@@ -0,0 +1,17 @@
+use SNMP;
+
+$SNMP::auto_init_mib = 0;
+
+$sess = new SNMP::Session();
+
+sub poller {
+ # VarList is undefined if TIMEOUT occured
+ if (!defined($_[1])) { die "request timed out[$_[0]->{ErrorStr}]\n"; }
+ if ($i++>100000) { die "completed 500 polls\n"; }
+ #print $_[1][0]->tag, " = ", $_[1][0]->val, "\n";
+ $_[0]->get($_[1], [\&poller, $_[0]]);
+}
+
+$sess->get([[".1.3.6.1.2.1.1.3.0"]], [\&poller, $sess]);
+
+SNMP::MainLoop();
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/async2.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/async2.pl
new file mode 100644
index 0000000000..f0e41b326e
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/async2.pl
@@ -0,0 +1,19 @@
+use SNMP;
+
+$SNMP::auto_init_mib = 0;
+
+$sess = new SNMP::Session();
+
+sub poller_handler {
+ if (++$i>500) { die "completed 500 polls\n"; };
+ # VarList is undefined if TIMEOUT occured
+ if (!defined($_[1])) {
+ warn "request timed out[$_[0]->{ErrorStr}]\n";
+ return;
+ }
+# print "$i) ",$_[1][0]->tag, " = ", $_[1][0]->val, "\n";
+}
+
+# $sess->get([[".1.3.6.1.2.1.1.3.0"]], [\&poller_handler, $sess]);
+
+SNMP::MainLoop(.1,sub {for (1..50) {$sess->get([['.1.3.6.1.2.1.1.3.0']], [\&poller_handler, $sess]);} });
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/bulkwalk.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/bulkwalk.pl
new file mode 100644
index 0000000000..3ee2412072
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/bulkwalk.pl
@@ -0,0 +1,121 @@
+use SNMP;
+
+# Hard-coded hostname and community. This is icky, but I didn't want to
+# muddle the example with parsing command line arguments. Deal with it. -r
+#
+my $hostname='localhost';
+my $port='161';
+my $community='public';
+
+$SNMP::debugging = 0;
+$SNMP::dump_packet = 0;
+
+$sess = new SNMP::Session( 'DestHost' => $hostname,
+ 'Community' => $community,
+ 'RemotePort' => $port,
+ 'Timeout' => 300000,
+ 'Retries' => 3,
+ 'Version' => '2c',
+ 'UseLongNames' => 1, # Return full OID tags
+ 'UseNumeric' => 1, # Return dotted decimal OID
+ 'UseEnums' => 0, # Don't use enumerated vals
+ 'UseSprintValue' => 0); # Don't pretty-print values
+
+die "Cannot create session: ${SNMP::ErrorStr}\n" unless defined $sess;
+
+# Set up a list of two non-repeaters and some repeated variables.
+#
+# IMPORTANT NOTE:
+#
+# The 'get' performed for non-repeaters is a "GETNEXT" (the non-repeater
+# requests are not fulfilled with SNMP GET's). This means that you must
+# ask for the lexicographically preceeding variable for non-repeaters.
+#
+# For most branches (i.e. 'sysUpTime'), this "just works" -- be sure you
+# don't ask for an instance, and the response will be as expected. However,
+# if you want a specific variable instance (i.e. 'ifSpeed.5'), you must
+# ask for the _preceeding_ variable ('ifSpeed.4' in this example).
+#
+# See section 4.2.3 of RFC 1905 for more details on GETBULK PDU handling.
+#
+
+my $vars = new SNMP::VarList( ['sysUpTime'], # Nonrepeater variable
+ ['ifNumber'], # Nonrepeater variable
+ ['ifSpeed'], # Repeated variable
+ ['ifDescr'] ); # Repeated variable.
+
+# Do the bulkwalk of the two non-repeaters, and the repeaters. Ask for no
+# more than 8 values per response packet. If the caller already knows how
+# many instances will be returned for the repeaters, it can ask only for
+# that many repeaters.
+#
+@resp = $sess->bulkwalk(2, 8, $vars);
+die "Cannot do bulkwalk: $sess->{ErrorStr} ($sess->{ErrorNum})\n"
+ if $sess->{ErrorNum};
+
+# Print out the returned response for each variable.
+for $vbarr ( @resp ) {
+ # Determine which OID this request queried. This is kept in the VarList
+ # reference passed to bulkwalk().
+ $oid = $$vars[$i++]->tag();
+
+ # Count the number of responses to this query. The count will be 1 for
+ # non-repeaters, 1 or more for repeaters.
+ $num = scalar @$vbarr;
+ print "$num responses for oid $oid: \n";
+
+ # Display the returned list of varbinds using the SNMP::Varbind methods.
+ for $v (@$vbarr) {
+ printf("\t%s = %s (%s)\n", $v->name, $v->val, $v->type);
+ }
+ print "\n";
+}
+
+#
+# Now do the same bulkwalk again, but in asynchronous mode. Set up a Perl
+# callback to receive the reference to the array of arrays of Varbind's for
+# the return value, and pass along the $vars VarList to it. This allows us
+# to print the oid tags (the callback code is almost the same as above).
+#
+# First, define the Perl callback to be called when the bulkwalk completes.
+# The call to SNMP::finish() will cause the SNMP::MainLoop() to return once
+# the callback has completed, so that processing can continue.
+#
+sub callback {
+ my ($vars, $values) = @_;
+
+ for $vbarr ( @$values ) {
+ # Determine which OID this request queried. This is kept in the
+ # '$vars' VarList reference passed to the Perl callback by the
+ # asynchronous callback.
+ $oid = (shift @$vars)->tag();
+
+ # Count the number of responses to this query. The count will be 1 for
+ # non-repeaters, 1 or more for repeaters.
+ $num = scalar @$vbarr;
+ print "$num responses for oid $oid: \n";
+
+ # Display the returned list of varbinds using the SNMP::Varbind methods.
+ for $v (@$vbarr) {
+ printf("\t%s = %s (%s)\n", $v->name, $v->val, $v->type);
+ }
+ print "\n";
+ }
+
+ SNMP::finish();
+}
+
+# The actual bulkwalk request is done here. Note that the $vars VarList
+# reference will be passed to the Perl callback when the bulkwalk completes.
+#
+my $reqid = $sess->bulkwalk(2, 8, $vars, [ \&callback, $vars ]);
+die "Cannot do async bulkwalk: $sess->{ErrorStr} ($sess->{ErrorNum})\n"
+ if $sess->{ErrorNum};
+
+# Now drop into the SNMP event loop and await completion of the bulkwalk.
+# The call to SNMP::finish() in &callback will make the SNMP::MainLoop()
+# return to the caller.
+#
+SNMP::MainLoop();
+
+exit 0;
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/ipforward.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/ipforward.pl
new file mode 100644
index 0000000000..de61e40201
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/ipforward.pl
@@ -0,0 +1,30 @@
+use SNMP;
+$SNMP::use_enums = 1;
+
+my $host = shift;
+my $comm = shift;
+$sess = new SNMP::Session(DestHost => $host, Community => $comm);
+
+$vars = new SNMP::VarList( ['ipRouteIfIndex'], ['ipRouteType'],
+ ['ipRouteProto'], ['ipRouteMask'],
+ ['ipRouteNextHop'], ['ipRouteAge'],
+ ['ipRouteMetric1']);
+
+format STDOUT_TOP =
+ Destination Next Hop Mask Proto Age Metric
+--------------- --------------- -------------- ------- -------- ------
+.
+
+format STDOUT =
+@<<<<<<<<<<<<<< @<<<<<<<<<<<<<< @<<<<<<<<<<<<< @|||||| @||||||| @|||||
+$dest, $nhop, $mask, $proto, $age, $metric
+.
+
+for (($index,$type,$proto,$mask,$nhop,$age,$metric) = $sess->getnext($vars);
+ $$vars[0]->tag eq 'ipRouteIfIndex' and not $sess->{ErrorStr};
+ ($index,$type,$proto,$mask,$nhop,$age,$metric) = $sess->getnext($vars)) {
+ $dest = $$vars[0]->iid;
+ write;
+}
+
+print "$sess->{ErrorStr}\n";
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/mibtree.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/mibtree.pl
new file mode 100644
index 0000000000..0616126208
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/mibtree.pl
@@ -0,0 +1,20 @@
+use SNMP;
+$SNMP::save_descriptions = 1; # must be set prior to mib initialization
+SNMP::initMib(); # parses default list of Mib modules from default dirs
+
+# read dotted decimal oid or symbolic name to look up
+# partial name will be searched and all matches returned
+$val = shift || die "supply partial or complete object name or identifier\n";
+
+if ($node = $SNMP::MIB{$val}) {
+ print "$node:$node->{label} [$node->{objectID}]\n";
+ while (($k,$v) = each %$node) {
+ print "\t$k => $v\n";
+ }
+} else {
+ while (($k,$v) = each %SNMP::MIB) {
+ print "$v->{label} [$v->{obj}]\n" #accepts unique partial key(objectID)
+ if $k =~ /$val/ or $v->{label} =~ /$val/;
+ }
+}
+
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/mibwalk.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/mibwalk.pl
new file mode 100644
index 0000000000..ae743d3245
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/mibwalk.pl
@@ -0,0 +1,17 @@
+# snmpwalk of entire MIB
+# stop on error at end of MIB
+
+use SNMP 1.8;
+$SNMP::use_sprint_value = 1;
+my $host = shift || localhost;
+my $comm = shift || public;
+
+$sess = new SNMP::Session(DestHost => $host, Community => $comm);
+
+$var = new SNMP::Varbind([]);
+
+do {
+ $val = $sess->getnext($var);
+ print "$var->[$SNMP::Varbind::tag_f].$var->[$SNMP::Varbind::iid_f] = ",
+ "$var->[$SNMP::Varbind::val_f]\n";
+} until ($sess->{ErrorStr});
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/pingmib.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/pingmib.pl
new file mode 100644
index 0000000000..22dbe69007
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/pingmib.pl
@@ -0,0 +1,36 @@
+use strict;
+use SNMP;
+
+my $target = shift || die "no ping target supplied\n"; # numeric ip address
+my $host = shift || 'localhost';
+my $community = shift || 'private';
+
+{
+ my $sess = new SNMP::Session (DestHost => $host,
+ Community => $community,
+ Retries => 1);
+
+ my $dec = pack("C*",split /\./, $target);
+ my $oid = ".1.3.6.1.4.1.9.9.16.1.1.1";
+ my $row = "300";
+
+ $sess->set([
+ ["$oid.16", $row, 6, "INTEGER"],
+ ["$oid.16", $row, 5, "INTEGER"],
+ ["$oid.15", $row, "MoNDS", "OCTETSTR"],
+ ["$oid.2", $row, 1, "INTEGER"],
+ ["$oid.4", $row, 20, "INTEGER"],
+ ["$oid.5", $row, 150, "INTEGER"],
+ ["$oid.3", $row, $dec, "OCTETSTR"]]);
+
+ $sess->set([["$oid.16", $row, 1, "INTEGER"]]);
+ sleep 30;
+ my ($sent, $received, $low, $avg, $high, $completed) = $sess->get([
+ ["$oid.9", $row], ["$oid.10", $row], ["$oid.11", $row],
+ ["$oid.12", $row], ["$oid.13", $row], ["$oid.14", $row]]);
+
+ printf "Packet loss: %d% (%d/%d)\n", (100 * ($sent-$received)) / $sent,
+ $received, $sent;
+ print "Average delay $avg (low: $low high: $high)\n";
+ $sess->set(["$oid.16", $row, 6, "INTEGER"]);
+}
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/tablewalk.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/tablewalk.pl
new file mode 100644
index 0000000000..6fe344cb3b
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/tablewalk.pl
@@ -0,0 +1,19 @@
+# snmpwalk of a single table
+# getnext of 3 columns from ipAddrEntry table
+# stop after last row in table
+
+use SNMP 1.8;
+
+my $host = shift || localhost;
+my $comm = shift || public;
+
+my $sess = new SNMP::Session ( DestHost => $host, Community => $comm );
+
+my $vars = new SNMP::VarList([ipAdEntAddr],[ipAdEntIfIndex],[ipAdEntNetMask]);
+
+for (@vals = $sess->getnext($vars);
+ $vars->[0]->tag =~ /ipAdEntAddr/ # still in table
+ and not $sess->{ErrorStr}; # and not end of mib or other error
+ @vals = $sess->getnext($vars)) {
+ print " ($vals[1]) $vals[0]/$vals[2]\n";
+}
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/testleak.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/testleak.pl
new file mode 100644
index 0000000000..495f33b0f4
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/testleak.pl
@@ -0,0 +1,19 @@
+use SNMP 1.6;
+
+$host = shift;
+unless ($host) {
+ $| = 1; print "enter SNMP host address: "; $| = 0;
+ chomp($host = <STDIN>);
+}
+
+$obj = new SNMP::Session DestHost, $host;
+
+while (){
+print $obj->get(["ifNumber",0]);
+ open(COM,"ps -u$$|") || die;
+ @bar = <COM>;
+ $siz = (split(' ',$bar[1]))[4];
+ $rss = (split(' ',$bar[1]))[5];
+ close(COM);
+ print "siz = $siz, rss = $rss\n";
+}
diff --git a/cleopatre/application/spidnetsnmp/perl/SNMP/examples/trap-example.pl b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/trap-example.pl
new file mode 100644
index 0000000000..8b8913fb8d
--- /dev/null
+++ b/cleopatre/application/spidnetsnmp/perl/SNMP/examples/trap-example.pl
@@ -0,0 +1,94 @@
+use strict;
+use vars qw();
+use SNMP qw();
+
+&SNMP::initMib();
+
+&SNMP::loadModules(
+ 'RFC2127-MIB',
+ );
+
+sub trap_call_setup;
+sub trap_dummy;
+
+#
+# should eventually get these out of the MIB...
+#
+my %dispatch_table = (
+ 'isdnMibCallInformation', \&trap_call_setup,
+ '.', \&trap_dummy,
+);
+
+sub trap_dispatcher
+{
+ my $session = shift;
+ my $ref = shift;
+ my $trapType;
+ my ($reqid, $addr, $community);
+
+ # if this is a timeout, then there will be no args...
+ if (defined($ref)) {
+ $ref->[1]->[2] = SNMP::translateObj($ref->[1]->val);
+ $trapType = $ref->[1]->val;
+ my $args = shift;
+ ($reqid, $addr, $community) = @{$args};
+ } else {
+ $trapType = 'timeout';
+ }
+
+ if (defined($dispatch_table{$trapType})) {
+ &{$dispatch_table{$trapType}}($session, $ref);
+ } elsif (defined($dispatch_table{'.'})) {
+ &{$dispatch_table{'.'}}($session, $ref);
+ } else {
+ # don't do anything... silently discard.
+ }
+}
+
+sub trap_dummy
+{
+ my $session = shift;
+ my $ref = shift;
+
+ my $trapType = $ref->[1]->val;
+
+ warn "unexpected trap " . $trapType;
+}
+
+
+sub trap_call_setup
+{
+ my $session = shift;
+ my $varlist = shift;
+ my $args = shift;
+
+ my $ifIndex = $varlist->[2]->val;
+ my $isdnBearerOperStatus = $varlist->[3]->val;
+ my $isdnBearerPeerAddress = $varlist->[4]->val;
+ my $isdnBearerPeerSubAddress = $varlist->[5]->val;
+ my $isdnBearerInfoType = $varlist->[6]->val;
+ my $isdnBearerCallOrigin = $varlist->[5]->val;
+
+ my ($reqid, $ipaddr, $community) = @{$args};
+
+ printf "Call from %s", $isdnBearerPeerAddress;
+ printf "*%s", $isdnBearerPeerSubAddress if ($isdnBearerPeerSubAddress ne '');
+ printf "\n";
+}
+
+my $session = new SNMP::Session(
+ DestHost => 'udp:162',
+ LocalPort => 1,
+ Version => '2c',
+ UseEnums => 0,
+ );
+
+if (!defined($session)) {
+ die "can't create listener session";
+}
+
+# otherwise assume that ErrorNum is zero...
+
+$session->SNMP::_catch([\&trap_dispatcher, $session]);
+
+&SNMP::MainLoop();