2013-04-25 83 views
3

我需要Python中的SNMP代理,該監聽某個端口上並響應SNMP的基本命令(如GTE,SET,GETNEXT ...等)SNMP代理在Python

如果任何一個有代碼,請回復此帖。

+0

[pysnmp command responder](http://pysnmp.sourceforge.net/examples/current/v3arch/agent/cmdrsp/v3-multiple-users.html)和[查看zabbix的nullege源代碼](http: //www.nullege.com/codes/show/src%40z%40a%40zabbixSNMP-creator-HEAD%40src%40zabbixsnmp_creator.py/34/pysnmp/python)。 pysnmp命令響應代碼將像魅力一樣工作 – 2013-04-25 11:39:24

+0

我檢查了Command Responder腳本,但我的目標是從txt文件中讀取OID並回復get/getnext等請求。如果設置請求來了,那麼它應該在文件中寫入OID。我沒有得到命令響應者如何讀取OID以及從哪裏獲取 – aloksinghk 2013-04-29 06:04:26

+0

[請仔細閱讀示例](http://pysnmp.sourceforge.net/examples/current/v3arch/agent/cmdrsp/v2c-custom-scalar-mib -objects.html),其中MIB由'mibBuilder.importSymbols('SNMP-V2-SMI')加載'。 – 2013-04-29 13:00:25

回答

-1
import socket 
import netsnmp 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) 
s.bind((HOST, PORT)) #give host ip and port no: 
s.listen(2) 

conn, addr = s.accept() 
data = conn.recv(1024) 
bindvariable = netsnmp.Varbind("give full oid here") 
xx = netsnmp.snmpget(bindvariable, Version = 2, DestHost = '192.168.0.216', Community='private') 

同樣可以使用snmpset也走

+1

這似乎有點混亂。我不是Python程序員,但上面的代碼似乎是 1.打開TCP套接字 2.發送SNMP獲取請求 這兩者都不與SNMP代理相關。請在閱讀相關問題之前閱讀相關的RFC(例如1905)以開發一些基本的SNMP知識。 – Jolta 2013-06-13 08:08:53

3

那裏pysnmp web-site的SNMP命令響應腳本的集合。

下面是一個簡單的基於PySNMP的命令響應程序,它使用/ tmp中的文本文件作爲MIB數據存儲。

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 

請記住,這個腳本只是一個初學者,它並不處理一些特殊情況。

+0

我檢查了Command Responder腳本,但我的目標是從txt文件讀取OID並回復get/getnext等請求。如果設置請求來了,那麼它應該在文件中寫入OID。我沒有得到命令響應者如何讀取OID和從哪裏讀取。 – aloksinghk 2013-04-29 06:01:20

+0

上面的腳本將值寫入以OID命名的文本文件中(在/ tmp中),並從GET處讀取值。這發生在readVars()和writeVars()方法中。 – 2013-05-06 14:19:51