Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SNMP Agent in Python

I required a SNMP agent in python, which listen on a particular port and and responds to basic SNMP command (like GTE, SET, GETNEXT ...etc)

If any one have code please reply on this post.

like image 365
aloksinghk Avatar asked Nov 03 '22 23:11

aloksinghk


1 Answers

There's a collection of SNMP Command Responder scripts at pysnmp web-site.

Here's a simple PySNMP-based Command Responder that uses text files in /tmp as a MIB data store.

from pysnmp.entity import engine, config
from pysnmp.entity.rfc3413 import cmdrsp, context
from pysnmp.carrier.asynsock.dgram import udp
from pysnmp.smi import instrum, error
from pysnmp.proto.api import v2c

snmpEngine = engine.SnmpEngine()

config.addSocketTransport(
    snmpEngine,
    udp.domainName,
    udp.UdpTransport().openServerMode(('127.0.0.1', 1161))
)

config.addV1System(snmpEngine, 'my-area', 'public', contextName='my-context')

config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (1,3,6), (1,3,6))

snmpContext = context.SnmpContext(snmpEngine)

class FileInstrumController(instrum.AbstractMibInstrumController):
    def readVars(self, vars, acInfo=(None, None)):
        try:
            return [ (o,v2c.OctetString(open('/tmp/%s.txt' % o, 'r').read())) for o,v in vars ]
        except IOError:
            raise error.SmiError

    def writeVars(self, vars, acInfo=(None, None)):
        try:
            for o,v in vars:
                open('/tmp/%s.txt' % o, 'w').write(str(v))
            return vars
        except IOError:
            raise error.SmiError

snmpContext.registerContextName(
    v2c.OctetString('my-context'),          # Context Name
    FileInstrumController()                 # Management Instrumentation
)

cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
cmdrsp.SetCommandResponder(snmpEngine, snmpContext)

snmpEngine.transportDispatcher.jobStarted(1)

try:
    snmpEngine.transportDispatcher.runDispatcher()
except:
    snmpEngine.transportDispatcher.closeDispatcher()
    raise

Keep in mind that this script is just a starter for you, it does not handle some corner cases.

like image 153
Ilya Etingof Avatar answered Nov 09 '22 15:11

Ilya Etingof