Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require 'date'
- require 'resolv'
- require 'uri'
- require 'net/telnet'
- ### external gems
- require 'snmp'
- class KUtil
- def self.vblist_to_hash (varbind_list)
- r = {}
- varbind_list.each { |x| r[x.name.to_s] = x.value }
- return r
- end
- def self.snmpget (callee, oid)
- x = self.snmpgetbulk(callee, [ oid ] )
- return x[oid] if ! x.nil?
- return nil
- end
- def self.snmpgetbulk (callee, oids)
- if ( callee.kind_of? SNMP::Manager ) then
- begin
- return self.vblist_to_hash(callee.get(oids).varbind_list)
- rescue SNMP::RequestTimeout => e
- end
- return nil
- end
- if ( callee.kind_of? String ) then
- result = nil
- SNMP::Manager.open(:host => callee, :mib_modules => [], :timeout => 3, :retries => 0, :read_community => 'public') { |m|
- result = self.snmpgetbulk(m, oids)
- }
- return result
- end
- puts "#{self.name}.#{__method__} TypeError #{callee.inspect}"
- raise TypeError
- end
- def self.snmpset (callee, oid, value)
- x = self.snmpsetbulk(callee, { oid => value } )
- return x[oid] if ! x.nil?
- return nil
- end
- def self.snmpsetbulk (callee, hashed_oid_value_pairs)
- if ( callee.kind_of? SNMP::Manager ) then
- vblist = hashed_oid_value_pairs.keys.map { |k|
- value = hashed_oid_value_pairs[k]
- if ( value.kind_of? String ) then
- value = SNMP::OctetString.new(value)
- end
- if ( value.kind_of? Fixnum ) then
- value = SNMP::Integer.new(value)
- end
- SNMP::VarBind.new(k, value)
- }
- begin
- return self.vblist_to_hash(callee.set(vblist).varbind_list)
- rescue SNMP::RequestTimeout => e
- end
- return nil
- end
- if ( callee.kind_of? String ) then
- result = nil
- SNMP::Manager.open(:host => callee, :mib_modules => [], :timeout => 3, :retries => 0, :write_community => 'private') { |m|
- result = self.snmpsetbulk(m, hashed_oid_value_pairs)
- }
- return result
- end
- puts "#{self.name}.#{__method__} TypeError #{callee.inspect}"
- raise TypeError
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement