Class: Net::SNMP::ManagerRepl

Inherits:
Object
  • Object
show all
Includes:
Net::SNMP
Defined in:
lib/net/snmp/repl/manager_repl.rb

Constant Summary collapse

NO_SESSION_PROMPT =
<<EOF

No manager sessions active
--------------------------

 - Use `manage(options)` to start a session.
 - run `? manage` for more details

EOF

Constants included from Net::SNMP

VERSION

Constants included from Constants

Constants::ASN_APPLICATION, Constants::ASN_BIT8, Constants::ASN_BIT_STR, Constants::ASN_BOOLEAN, Constants::ASN_CONSTRUCTOR, Constants::ASN_CONTEXT, Constants::ASN_COUNTER, Constants::ASN_COUNTER64, Constants::ASN_EXTENSION_ID, Constants::ASN_GAUGE, Constants::ASN_INTEGER, Constants::ASN_IPADDRESS, Constants::ASN_LONG_LEN, Constants::ASN_NSAP, Constants::ASN_NULL, Constants::ASN_OBJECT_ID, Constants::ASN_OCTET_STR, Constants::ASN_OPAQUE, Constants::ASN_PRIMITIVE, Constants::ASN_PRIVATE, Constants::ASN_SEQUENCE, Constants::ASN_SET, Constants::ASN_TIMETICKS, Constants::ASN_UINTEGER, Constants::ASN_UNIVERSAL, Constants::ASN_UNSIGNED, Constants::DUMP_PACKET, Constants::MAX_NAME_LEN, Constants::MAX_OID_LEN, Constants::MAX_STATS, Constants::MAX_SUBID, Constants::MIN_OID_LEN, Constants::NETSNMP_CALLBACK_OP_CONNECT, Constants::NETSNMP_CALLBACK_OP_DISCONNECT, Constants::NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE, Constants::NETSNMP_CALLBACK_OP_SEND_FAILED, Constants::NETSNMP_CALLBACK_OP_TIMED_OUT, Constants::OID_SNMP_TRAP_OID, Constants::OID_SYS_UP_TIME_INSTANCE, Constants::PARSE_PACKET, Constants::REPORT_STATS_LEN, Constants::REPORT_snmpInvalidMsgs_NUM, Constants::REPORT_snmpUnknownSecurityModels_NUM, Constants::REPORT_usmStatsDecryptionErrors_NUM, Constants::REPORT_usmStatsNotInTimeWindows_NUM, Constants::REPORT_usmStatsUnknownEngineIDs_NUM, Constants::REPORT_usmStatsUnknownUserNames_NUM, Constants::REPORT_usmStatsUnsupportedSecLevels_NUM, Constants::REPORT_usmStatsWrongDigests_NUM, Constants::SNMPERR_ABORT, Constants::SNMPERR_ASN_PARSE_ERR, Constants::SNMPERR_AUTHENTICATION_FAILURE, Constants::SNMPERR_BAD_ACL, Constants::SNMPERR_BAD_ADDRESS, Constants::SNMPERR_BAD_ASN1_BUILD, Constants::SNMPERR_BAD_COMMUNITY, Constants::SNMPERR_BAD_CONTEXT, Constants::SNMPERR_BAD_DST_PARTY, Constants::SNMPERR_BAD_ENG_ID, Constants::SNMPERR_BAD_LOCPORT, Constants::SNMPERR_BAD_NAME, Constants::SNMPERR_BAD_PARSE, Constants::SNMPERR_BAD_PARTY, Constants::SNMPERR_BAD_RECVFROM, Constants::SNMPERR_BAD_REPEATERS, Constants::SNMPERR_BAD_REPETITIONS, Constants::SNMPERR_BAD_SEC_LEVEL, Constants::SNMPERR_BAD_SEC_NAME, Constants::SNMPERR_BAD_SENDTO, Constants::SNMPERR_BAD_SESSION, Constants::SNMPERR_BAD_SRC_PARTY, Constants::SNMPERR_BAD_SUBID, Constants::SNMPERR_BAD_VERSION, Constants::SNMPERR_DECRYPTION_ERR, Constants::SNMPERR_GENERR, Constants::SNMPERR_INVALID_MSG, Constants::SNMPERR_KRB5, Constants::SNMPERR_KT_NOT_AVAILABLE, Constants::SNMPERR_LONG_OID, Constants::SNMPERR_MALLOC, Constants::SNMPERR_MAX, Constants::SNMPERR_MAX_SUBID, Constants::SNMPERR_NOAUTH_DESPRIV, Constants::SNMPERR_NOMIB, Constants::SNMPERR_NOT_IN_TIME_WINDOW, Constants::SNMPERR_NO_SOCKET, Constants::SNMPERR_NO_VARS, Constants::SNMPERR_NULL_PDU, Constants::SNMPERR_OID_NONINCREASING, Constants::SNMPERR_PROTOCOL, Constants::SNMPERR_RANGE, Constants::SNMPERR_SC_GENERAL_FAILURE, Constants::SNMPERR_SC_NOT_CONFIGURED, Constants::SNMPERR_SUCCESS, Constants::SNMPERR_TIMEOUT, Constants::SNMPERR_TOO_LONG, Constants::SNMPERR_UNKNOWN_ENG_ID, Constants::SNMPERR_UNKNOWN_OBJID, Constants::SNMPERR_UNKNOWN_PDU, Constants::SNMPERR_UNKNOWN_REPORT, Constants::SNMPERR_UNKNOWN_SEC_MODEL, Constants::SNMPERR_UNKNOWN_USER_NAME, Constants::SNMPERR_UNSUPPORTED_SEC_LEVEL, Constants::SNMPERR_USM_AUTHENTICATIONFAILURE, Constants::SNMPERR_USM_DECRYPTIONERROR, Constants::SNMPERR_USM_ENCRYPTIONERROR, Constants::SNMPERR_USM_GENERICERROR, Constants::SNMPERR_USM_NOTINTIMEWINDOW, Constants::SNMPERR_USM_PARSEERROR, Constants::SNMPERR_USM_UNKNOWNENGINEID, Constants::SNMPERR_USM_UNKNOWNSECURITYNAME, Constants::SNMPERR_USM_UNSUPPORTEDSECURITYLEVEL, Constants::SNMPERR_V1_IN_V2, Constants::SNMPERR_V2_IN_V1, Constants::SNMPERR_VALUE, Constants::SNMPERR_VAR_TYPE, Constants::SNMPV3_IGNORE_UNAUTH_REPORTS, Constants::SNMP_DEFAULT_ADDRESS, Constants::SNMP_DEFAULT_COMMUNITY_LEN, Constants::SNMP_DEFAULT_CONTEXT, Constants::SNMP_DEFAULT_ENTERPRISE_LENGTH, Constants::SNMP_DEFAULT_ERRINDEX, Constants::SNMP_DEFAULT_ERRSTAT, Constants::SNMP_DEFAULT_MSGID, Constants::SNMP_DEFAULT_REMPORT, Constants::SNMP_DEFAULT_REQID, Constants::SNMP_DEFAULT_RETRIES, Constants::SNMP_DEFAULT_SECMODEL, Constants::SNMP_DEFAULT_TIME, Constants::SNMP_DEFAULT_TIMEOUT, Constants::SNMP_DEFAULT_VERSION, Constants::SNMP_DETAIL_SIZE, Constants::SNMP_ENDOFMIBVIEW, Constants::SNMP_ERR_AUTHORIZATIONERROR, Constants::SNMP_ERR_BADVALUE, Constants::SNMP_ERR_COMMITFAILED, Constants::SNMP_ERR_GENERR, Constants::SNMP_ERR_INCONSISTENTNAME, Constants::SNMP_ERR_INCONSISTENTVALUE, Constants::SNMP_ERR_NOACCESS, Constants::SNMP_ERR_NOCREATION, Constants::SNMP_ERR_NOERROR, Constants::SNMP_ERR_NOSUCHNAME, Constants::SNMP_ERR_NOTWRITABLE, Constants::SNMP_ERR_READONLY, Constants::SNMP_ERR_RESOURCEUNAVAILABLE, Constants::SNMP_ERR_TOOBIG, Constants::SNMP_ERR_UNDOFAILED, Constants::SNMP_ERR_WRONGENCODING, Constants::SNMP_ERR_WRONGLENGTH, Constants::SNMP_ERR_WRONGTYPE, Constants::SNMP_ERR_WRONGVALUE, Constants::SNMP_FLAGS_DONT_PROBE, Constants::SNMP_FLAGS_LISTENING, Constants::SNMP_FLAGS_RESP_CALLBACK, Constants::SNMP_FLAGS_STREAM_SOCKET, Constants::SNMP_FLAGS_STRIKE1, Constants::SNMP_FLAGS_STRIKE2, Constants::SNMP_FLAGS_SUBSESSION, Constants::SNMP_FLAGS_USER_CREATED, Constants::SNMP_MAX_CONTEXT_SIZE, Constants::SNMP_MAX_ENG_SIZE, Constants::SNMP_MAX_MSG_V3_HDRS, Constants::SNMP_MAX_SEC_NAME_SIZE, Constants::SNMP_MSG_GET, Constants::SNMP_MSG_GETBULK, Constants::SNMP_MSG_GETNEXT, Constants::SNMP_MSG_INFORM, Constants::SNMP_MSG_REPORT, Constants::SNMP_MSG_RESPONSE, Constants::SNMP_MSG_SET, Constants::SNMP_MSG_TRAP, Constants::SNMP_MSG_TRAP2, Constants::SNMP_NOSUCHINSTANCE, Constants::SNMP_NOSUCHOBJECT, Constants::SNMP_SEC_LEVEL_AUTHNOPRIV, Constants::SNMP_SEC_LEVEL_AUTHPRIV, Constants::SNMP_SEC_LEVEL_NOAUTH, Constants::SNMP_SEC_PARAM_BUF_SIZE, Constants::SNMP_SESS_AUTHORITATIVE, Constants::SNMP_SESS_NONAUTHORITATIVE, Constants::SNMP_SESS_UNKNOWNAUTH, Constants::SNMP_VERSION_1, Constants::SNMP_VERSION_2c, Constants::SNMP_VERSION_3, Constants::STAT_ERROR, Constants::STAT_MPD_STATS_END, Constants::STAT_MPD_STATS_START, Constants::STAT_SNMPINASNPARSEERRS, Constants::STAT_SNMPINBADCOMMUNITYNAMES, Constants::STAT_SNMPINBADCOMMUNITYUSES, Constants::STAT_SNMPINBADVALUES, Constants::STAT_SNMPINBADVERSIONS, Constants::STAT_SNMPINGENERRS, Constants::STAT_SNMPINGETNEXTS, Constants::STAT_SNMPINGETREQUESTS, Constants::STAT_SNMPINGETRESPONSES, Constants::STAT_SNMPINNOSUCHNAMES, Constants::STAT_SNMPINPKTS, Constants::STAT_SNMPINREADONLYS, Constants::STAT_SNMPINSETREQUESTS, Constants::STAT_SNMPINTOOBIGS, Constants::STAT_SNMPINTOTALREQVARS, Constants::STAT_SNMPINTOTALSETVARS, Constants::STAT_SNMPINTRAPS, Constants::STAT_SNMPINVALIDMSGS, Constants::STAT_SNMPOUTBADVALUES, Constants::STAT_SNMPOUTGENERRS, Constants::STAT_SNMPOUTGETNEXTS, Constants::STAT_SNMPOUTGETREQUESTS, Constants::STAT_SNMPOUTGETRESPONSES, Constants::STAT_SNMPOUTNOSUCHNAMES, Constants::STAT_SNMPOUTPKTS, Constants::STAT_SNMPOUTSETREQUESTS, Constants::STAT_SNMPOUTTOOBIGS, Constants::STAT_SNMPOUTTRAPS, Constants::STAT_SNMPPROXYDROPS, Constants::STAT_SNMPSILENTDROPS, Constants::STAT_SNMPUNAVAILABLECONTEXTS, Constants::STAT_SNMPUNKNOWNCONTEXTS, Constants::STAT_SNMPUNKNOWNPDUHANDLERS, Constants::STAT_SNMPUNKNOWNSECURITYMODELS, Constants::STAT_SNMP_STATS_END, Constants::STAT_SNMP_STATS_START, Constants::STAT_SUCCESS, Constants::STAT_TARGET_STATS_END, Constants::STAT_TARGET_STATS_START, Constants::STAT_TIMEOUT, Constants::STAT_USMSTATSDECRYPTIONERRORS, Constants::STAT_USMSTATSNOTINTIMEWINDOWS, Constants::STAT_USMSTATSUNKNOWNENGINEIDS, Constants::STAT_USMSTATSUNKNOWNUSERNAMES, Constants::STAT_USMSTATSUNSUPPORTEDSECLEVELS, Constants::STAT_USMSTATSWRONGDIGESTS, Constants::STAT_USM_STATS_END, Constants::STAT_USM_STATS_START, Constants::USM_AUTH_KU_LEN, Constants::USM_PRIV_KU_LEN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Net::SNMP

init, initialized?, thread_safe, thread_safe=

Constructor Details

#initialize(session = nil) ⇒ ManagerRepl

Returns a new instance of ManagerRepl.



23
24
25
26
27
28
# File 'lib/net/snmp/repl/manager_repl.rb', line 23

def initialize(session=nil)
  @sessions = []
  if session
    @sessions << session
  end
end

Instance Attribute Details

#pduObject Also known as: response

Returns the value of attribute pdu.



4
5
6
# File 'lib/net/snmp/repl/manager_repl.rb', line 4

def pdu
  @pdu
end

#sessionsObject

Returns the value of attribute sessions.



4
5
6
# File 'lib/net/snmp/repl/manager_repl.rb', line 4

def sessions
  @sessions
end

Class Method Details

.start(session = nil) ⇒ Object



17
18
19
20
21
# File 'lib/net/snmp/repl/manager_repl.rb', line 17

def self.start(session=nil)
  pry_context = self.new(session)
  Pry.config.prompt_name = "snmp"
  pry_context.pry
end

Instance Method Details

#close(peername) ⇒ Object

Close the session with the given ‘peername`

Arguments

  • peername: May be a string, matching the peername, or an index. Run the ‘peer` command to see indexes & peernames for all active sessions

Example

[7] net-snmp2> peers

Currently Managing
------------------

[0] localhost:161
[1] localhost:162
[2] localhost:163

=> nil
[8] net-snmp2> close 'localhost:163'
=> "Closed session for peer: localhost:163"
[9] net-snmp2> close 1
=> "Closed session for peer: localhost:162"


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/net/snmp/repl/manager_repl.rb', line 81

def close(peername)
  case peername
  when Numeric
    index = peername
    if index < (sessions.length)
      session = sessions[index]
      session.close
      sessions.delete(session)
      "Closed session for peer: #{session.peername}"
    else
      "Invalid session index #{index}. Use `peers` to list active sessions."
    end
  when String
    session = sessions.find { |sess| sess.peername.to_s == peername.to_s }
    if (session)
      session.close
      sessions.delete(session)
      "Closed session for peer: #{session.peername}"
    else
      "No session active for '#{peername}'. Use `peers` to list active sessions."
    end
  end
end

#describe(oid) ⇒ Object

Prints a description of a MIB variable

Arguments

  • oid: May be either a numeric OID, or MIB variable name



137
138
139
140
# File 'lib/net/snmp/repl/manager_repl.rb', line 137

def describe(oid)
  nodes = [MIB[oid]]
  puts ERB.new(Net::SNMP::MIB::Templates::DESCRIBE, nil, "-").result(binding)
end

#describe_tree(oid) ⇒ Object

Prints a description of a the MIB subtree starting at root ‘oid`

Arguments

  • oid: May be either a numeric OID, or MIB variable name



147
148
149
150
151
# File 'lib/net/snmp/repl/manager_repl.rb', line 147

def describe_tree(oid)
  root = MIB[oid]
  nodes = [root] + root.descendants.to_a
  puts ERB.new(Net::SNMP::MIB::Templates::DESCRIBE, nil, "-").result(binding)
end

#get(oids, options = {}) ⇒ Object

Issue an SNMP GET Request to all active peers

Arguments

  • oids: A single oid, or an array of oids



158
159
160
161
162
163
164
165
# File 'lib/net/snmp/repl/manager_repl.rb', line 158

def get(oids, options = {})
  each_session do |session|
    @pdu = session.get(oids)
    @pdu.print
    puts "ERROR" if @pdu.error?
  end
  "GET"
end

#get_bulk(oids, options = {}) ⇒ Object

Issue an SNMP GETBULK Request to all active peers

Arguments

  • oids: A single oid, or an array of oids

  • options: A Hash accepting the typical options keys for a request, plus

+ non_repeaters: The number of non-repeated oids in the request
+ max_repititions: The maximum repititions to return for all repeaters

Note that the non-repeating varbinds must be added first.



190
191
192
193
194
195
196
197
# File 'lib/net/snmp/repl/manager_repl.rb', line 190

def get_bulk(oids, options = {})
  each_session do |session|
    @pdu = session.get_bulk(oids)
    @pdu.print
    puts "ERROR" if @pdu.error?
  end
  "GETBULK"
end

#get_next(oids, options = {}) ⇒ Object

Issue an SNMP GETNEXT Request to all active peers

Arguments

  • oids: A single oid, or an array of oids



172
173
174
175
176
177
178
179
# File 'lib/net/snmp/repl/manager_repl.rb', line 172

def get_next(oids, options = {})
  each_session do |session|
    @pdu = session.get_next(oids)
    @pdu.print
    puts "ERROR" if @pdu.error?
  end
  "GETNEXT"
end

#manage(options) ⇒ Object

Adds a session for managing options to the sessions list.

  • Note that all requests are sent to all active sessions, and their responses are all displayed under their peername.

  • You may close a session by calling ‘close(peername)`

Arguments

  • options: A Hash or String object + As a Hash, accepts the following keys

    - peername: ADDRESS_OF_AGENT
    - port: PORT (defaults to 161)
    - version:  '1'|'2c'|'3' (defaults to '2c')
    

    + As a string, implies the peername, with the version defaulting to ‘2c’

Examples

manage('192.168.1.5')
manage('192.168.1.5:162')
manage(peername: '192.168.1.5', port: 161, version '2c')
manage(peername: '192.168.1.5:161', version '2c')


51
52
53
54
55
# File 'lib/net/snmp/repl/manager_repl.rb', line 51

def manage(options)
  options = {:peername => options} if options.kind_of?(String)
  sessions << Session.open(options)
  "Opened session to manage peer: #{options[:peername]}"
end

#peersObject

List the peers currently being managed



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/net/snmp/repl/manager_repl.rb', line 106

def peers
  if sessions.count > 0
    puts
    puts "Currently Managing"
    puts "------------------"
    puts
    sessions.each_with_index do |session, i|
      puts "[#{i}] #{session.peername}"
    end
    puts
  else
    puts "No active sessions"
  end
  nil
end

#set(varbinds, options = {}) ⇒ Object

Issue an SNMP Set Request to all active peers

Arguments

  • varbinds: An single varbind, or an array of varbinds, each of which may be + An Array of length 3 ‘[oid, type, value]` + An Array of length 2 `[oid, value]` + A Hash `oid, type: type, value: value`

    * Hash syntax is the same as supported by PDU.add_varbind
    * If type is not supplied, it is infered by the value
    


223
224
225
226
227
228
229
230
# File 'lib/net/snmp/repl/manager_repl.rb', line 223

def set(varbinds, options = {})
  each_session do |session|
    @pdu = session.set(varbinds)
    @pdu.print
    puts "ERROR" if @pdu.error?
  end
  "SET"
end

#translate(oid) ⇒ Object

Translates a numerical oid to it’s MIB name, or a name to numerical oid

Arguments

  • oid: Either a string, as the numerical OID or MIB variable name,

    or an OID object
    


128
129
130
# File 'lib/net/snmp/repl/manager_repl.rb', line 128

def translate(oid)
  MIB.translate(oid)
end

#walk(oids, options = {}) ⇒ Object

Performs a walk on all active peers for each oid provided

Arguments

  • oids: A single oid, or an array of oids



204
205
206
207
208
209
210
211
# File 'lib/net/snmp/repl/manager_repl.rb', line 204

def walk(oids, options = {})
  each_session do |session|
    session.walk(oids).each { |oid, value|
      puts "#{MIB.translate(oid)}(#{oid}) = #{value}"
    }
  end
  "WALK"
end