Python 通过func向pysnmp发送多oid
目前我有:Python 通过func向pysnmp发送多oid,python,pysnmp,Python,Pysnmp,目前我有: def snmp_request(self,*oids): my_oids ='' for oid in oids: my_oids += '\'' + oid + '\',' print(my_oids) answer_list = list() cmdGen = cmdgen.CommandGenerato
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
非常感谢,如果您的输入OID是一个Python字符串序列,那么您应该将其传递给nextCmd(),如下所示:
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
cmdgen.CommunityData(self.community),
cmdgen.UdpTransportTarget((self.ip, 161),20,1),
*oids
)
不需要在OID中添加额外的引号或逗号