Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/lib/snmp/agent.rb
Views: 11766
#1# Copyright (c) 2004 David R. Halliday2# All rights reserved.3#4# This SNMP library is free software. Redistribution is permitted under the5# same terms and conditions as the standard Ruby distribution. See the6# COPYING file in the Ruby distribution for details.7#89require 'snmp'10require 'socket'11require 'logger'1213module SNMP1415class Agent #:nodoc:all1617def initialize(listen_port=161, max_packet=8000)18@log = Logger.new(STDOUT)19@log.level = Logger::DEBUG20@max_packet = max_packet21@socket = UDPSocket.open22@socket.bind(nil, listen_port)23@mib = MIB::SystemMIB.new24end2526def start27@log.info "SNMP agent running"28loop do29begin30data, remote_info = @socket.recvfrom(@max_packet)31puts "Received #{data.length} bytes"32p data33message = Message.decode(data)34case message.pdu35when GetRequest36response = message.response37response.pdu.varbind_list.each do |v|38v.value = @mib.get(v.name)39end40when SetRequest41response = message.response42else43raise "invalid message #{message.to_s}"44end45puts "Responding to #{remote_info[3]}:#{remote_info[1]}"46encoded_message = response.encode47n=@socket.send(encoded_message, 0, remote_info[3], remote_info[1])48p encoded_message49rescue => e50@log.error e51shutdown52end53end54end5556def shutdown57@log.info "SNMP agent stopping"58@socket.close59exit60end6162alias stop :shutdown6364end6566end6768if $0 == __FILE__69agent = SNMP::Agent.new(1061)70trap("INT") { agent.shutdown }71agent.start72end73747576