PageRenderTime 133ms CodeModel.GetById 36ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/snmpset.pl

http://net-snmp-perl-module.googlecode.com/
Perl | 178 lines | 126 code | 31 blank | 21 comment | 15 complexity | 0e5abc74d9e4398fa677d92574a4343f MD5 | raw file
Possible License(s): AGPL-1.0
  1. #! /bin/env perl
  2. # ============================================================================
  3. # $Id: snmpset.pl,v 6.0 2009-09-09 15:05:33 dtown Exp $
  4. # Copyright (c) 2000-2009 David M. Town <dtown@cpan.org>
  5. # All rights reserved.
  6. # This program is free software; you may redistribute it and/or modify it
  7. # under the same terms as the Perl 5 programming language system itself.
  8. # ============================================================================
  9. use strict;
  10. use warnings;
  11. use Net::SNMP 6.0 qw( :asn1 snmp_type_ntop DEBUG_ALL );
  12. use Getopt::Std;
  13. our $SCRIPT = 'snmpset';
  14. our $VERSION = 'v6.0.0';
  15. our %OPTS;
  16. # Validate the command line options.
  17. if (!getopts('a:A:c:dD:E:m:n:p:r:t:u:v:x:X:', \%OPTS)) {
  18. usage();
  19. }
  20. # Do we have enough information?
  21. if (@ARGV < 4) {
  22. usage();
  23. }
  24. # Create the SNMP session.
  25. my ($s, $e) = Net::SNMP->session(
  26. -hostname => shift,
  27. exists($OPTS{a}) ? (-authprotocol => $OPTS{a}) : (),
  28. exists($OPTS{A}) ? (-authpassword => $OPTS{A}) : (),
  29. exists($OPTS{c}) ? (-community => $OPTS{c}) : (),
  30. exists($OPTS{D}) ? (-domain => $OPTS{D}) : (),
  31. exists($OPTS{d}) ? (-debug => DEBUG_ALL) : (),
  32. exists($OPTS{m}) ? (-maxmsgsize => $OPTS{m}) : (),
  33. exists($OPTS{p}) ? (-port => $OPTS{p}) : (),
  34. exists($OPTS{P}) ? (-protocol => $OPTS{P}) : (),
  35. exists($OPTS{r}) ? (-retries => $OPTS{r}) : (),
  36. exists($OPTS{t}) ? (-timeout => $OPTS{t}) : (),
  37. exists($OPTS{u}) ? (-username => $OPTS{u}) : (),
  38. exists($OPTS{v}) ? (-version => $OPTS{v}) : (),
  39. exists($OPTS{x}) ? (-privprotocol => $OPTS{x}) : (),
  40. exists($OPTS{X}) ? (-privpassword => $OPTS{X}) : (),
  41. );
  42. # Was the session created?
  43. if (!defined $s) {
  44. abort($e);
  45. }
  46. # Convert the ASN.1 types to the respresentation expected by Net::SNMP.
  47. if (convert_asn1_types(\@ARGV)) {
  48. usage();
  49. }
  50. my @args = (
  51. exists($OPTS{E}) ? (-contextengineid => $OPTS{E}) : (),
  52. exists($OPTS{n}) ? (-contextname => $OPTS{n}) : (),
  53. -varbindlist => \@ARGV,
  54. );
  55. # Send the SNMP message.
  56. if (!defined $s->set_request(@args)) {
  57. abort($s->error());
  58. }
  59. # Print the results.
  60. for ($s->var_bind_names()) {
  61. printf "%s = %s: %s\n",
  62. $_,
  63. snmp_type_ntop($s->var_bind_types()->{$_}),
  64. $s->var_bind_list()->{$_};
  65. }
  66. # Close the session.
  67. $s->close();
  68. exit 0;
  69. # [functions] ----------------------------------------------------------------
  70. sub convert_asn1_types
  71. {
  72. my ($argv) = @_;
  73. # Mapping table: { "user input character" => constant byte value }
  74. my %asn1_types = (
  75. 'a' => IPADDRESS,
  76. 'c' => COUNTER32,
  77. 'C' => COUNTER64,
  78. 'g' => GAUGE32,
  79. 'h' => OCTET_STRING,
  80. 'i' => INTEGER32,
  81. 'o' => OBJECT_IDENTIFIER,
  82. 'p' => OPAQUE,
  83. 's' => OCTET_STRING,
  84. 't' => TIMETICKS,
  85. );
  86. # Expect [OBJECT IDENTIFIER, ASN.1 type, object value] combination.
  87. if ((ref($argv) ne 'ARRAY') || (scalar(@{$argv}) % 3)) {
  88. return 1;
  89. }
  90. for (my $i = 0; $i < scalar @{$argv}; $i += 3) {
  91. if (exists $asn1_types{$argv->[$i+1]}) {
  92. if ($argv->[$i+1] eq 'h') {
  93. if ($argv->[$i+2] =~ m/^(?:0x)?([A-F\d]+)$/i) {
  94. # Convert hexadecimal string.
  95. $argv->[$i+2] = pack 'H*', length($1) % 2 ? '0'.$1 : $1;
  96. } else {
  97. abort(sprintf q{The string "%s" is is expected in } .
  98. q{hexadecimal format for type 'h'},
  99. $argv->[$i+2]);
  100. }
  101. }
  102. $argv->[$i+1] = $asn1_types{$argv->[$i+1]};
  103. } else {
  104. abort(sprintf 'The ASN.1 type "%s" is unknown', $argv->[$i+1]);
  105. }
  106. }
  107. return 0;
  108. }
  109. sub abort
  110. {
  111. printf "$SCRIPT: " . ((@_ > 1) ? shift(@_) : '%s') . ".\n", @_;
  112. exit 1;
  113. }
  114. sub usage
  115. {
  116. print << "USAGE";
  117. $SCRIPT $VERSION
  118. Copyright (c) 2000-2009 David M. Town. All rights reserved.
  119. Usage: $SCRIPT [options] <hostname> <oid> <type> <value> [...]
  120. Options: -v 1|2c|3 SNMP version
  121. -d Enable debugging
  122. SNMPv1/SNMPv2c:
  123. -c <community> Community name
  124. SNMPv3:
  125. -u <username> Username (required)
  126. -E <engineid> Context Engine ID
  127. -n <name> Context Name
  128. -a <authproto> Authentication protocol <md5|sha>
  129. -A <password> Authentication password
  130. -x <privproto> Privacy protocol <des|3des|aes>
  131. -X <password> Privacy password
  132. Transport Layer:
  133. -D <domain> Domain <udp|udp6|tcp|tcp6>
  134. -m <octets> Maximum message size
  135. -p <port> Destination port
  136. -r <attempts> Number of retries
  137. -t <secs> Timeout period
  138. Valid type values:
  139. a - IpAddress i - INTEGER
  140. c - Counter o - OBJECT IDENTIFIER
  141. C - Counter64 p - Opaque
  142. g - Gauge/Unsigned32 s - OCTET STRING
  143. h - OCTET STRING (hex) t - TimeTicks
  144. USAGE
  145. exit 1;
  146. }
  147. # ============================================================================