#!/usr/bin/env python # -*- coding: utf-8 -*- from twisted.internet import reactor, defer from pysnmp.entity import engine, config from pysnmp.carrier.twisted import dispatch from pysnmp.carrier.twisted.dgram import udp from pysnmp.entity.rfc3413.twisted import cmdgen def receiveResponse((errorIndication, errorStatus, errorIndex, varBinds)): if errorIndication or errorStatus: print 'Error: ', errorIndication, errorStatus.prettyPrint(), errorIndex return for oid, val in varBinds: if val is None: print oid.prettyPrint() else: print '%s = %s' % (oid.prettyPrint(), val.prettyPrint()) snmpEngine = engine.SnmpEngine() snmpEngine.registerTransportDispatcher(dispatch.TwistedDispatcher()) config.addSocketTransport(snmpEngine, udp.domainName, udp.UdpTwistedTransport().openClientMode()) host = { 'host1' : { 'community' : 'public', 'host' : '10.24.0.1' }, 'host2' : { 'community' : 'public', 'host' : '10.24.0.9' }, } getCmdGen = cmdgen.GetCommandGenerator() for h in host: print host[h] user = '%s-%s' % (h, 'user') param = '%s-%s' % (h, 'param') target = '%s-%s' % (h, 'target') config.addV1System(snmpEngine, user, host[h]['community'], transportTag=h) config.addTargetParams(snmpEngine, param, user, 'noAuthNoPriv', 1) config.addTargetAddr( snmpEngine, target, config.snmpUDPDomain, (host[h]['host'], 161), param, tagList=h) df = getCmdGen.sendReq( snmpEngine, target, (((1,3,6,1,2,1,1,1,0), None),) ) df.addCallback(receiveResponse) reactor.run()