通过函数将多个OID发送到pysnmp
目前我有这个:
def snmp_request(self,*oids):
my_oids =''
for oid in oids:
my_oids += '\'' + oid + '\','
print(my_oids)
answer_list = list()
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData(self.community),
cmdgen.UdpTransportTarget((self.ip, 161),20,1),
my_oids
)
if errorIndication:
return (errorIndication)
else:
if errorStatus:
return ('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
)
)
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
answer_list.append( val.prettyPrint())
return answer_list
打印出来的内容是:
'1.3.6.1.2.1.31.1.1.1.18','1.3.6.1.2.1.2.2.1.2',
但是它不工作……pysnmp不理解这个请求 -_-
另外,这个解决方案是可以的:
def snmp_request(self,*oids):
my_oids =''
for oid in oids:
my_oids += '\'' + oid + '\','
print(my_oids)
answer_list = list()
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData(self.community),
cmdgen.UdpTransportTarget((self.ip, 161),20,1),
'1.3.6.1.2.1.31.1.1.1.18','1.3.6.1.2.1.2.2.1.2',
)
if errorIndication:
return (errorIndication)
else:
if errorStatus:
return ('%s at %s' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
)
)
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
answer_list.append( val.prettyPrint())
return answer_list
但我必须在我的函数里写每一个OID,这样做非常没用,为什么我不能像我想的那样发送很多OID呢?
最好的问候,
1 个回答
2
如果你的输入是一些Python字符串的序列,你只需要这样把它传给nextCmd():
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData(self.community),
cmdgen.UdpTransportTarget((self.ip, 161),20,1),
*oids
)
不需要在OID前后加额外的引号或逗号。