Python 如何返回使用PySNMP完成的循环中得到的两个值?
我不熟悉PySNMP和Python,但对它很感兴趣 我从PySNMP文档中获取了一个脚本,并将其作为一个名为SnmpGetNext()的python函数 最后,我想做的是从nextCmd中获取两个OID,它应该为我提供1.3.6.1.2.1.2.2.2.1.14.1和.2 当我这样做时,它会得到两个值并将其保存到字典中,这很好Python 如何返回使用PySNMP完成的循环中得到的两个值?,python,python-3.x,pysnmp,Python,Python 3.x,Pysnmp,我不熟悉PySNMP和Python,但对它很感兴趣 我从PySNMP文档中获取了一个脚本,并将其作为一个名为SnmpGetNext()的python函数 最后,我想做的是从nextCmd中获取两个OID,它应该为我提供1.3.6.1.2.1.2.2.2.1.14.1和.2 当我这样做时,它会得到两个值并将其保存到字典中,这很好 for varBindTableRow in varBindTable: for name, val in varBindTableRow: re
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
result2 = {}
result2['OID']= str(val)
print(result2
但是,我希望能够返回它们,并在以后使用此函数将其用于更多OID,如果我返回result2,这将只返回第一个值
我试着将它作为字符串通过字典传递给OID
from pysnmp.entity.rfc3413.oneliner import cmdgen
host = "demo.snmplabs.com"
community = "public"
test1 = ".1.3.6.1.2.1.2.2.1.14"
def SnmpGetNext(host,community,oid):
errorIndication, errorStatus, errorIndex, \
varBindTable = cmdgen.CommandGenerator().nextCmd(
cmdgen.CommunityData('test-agent', community),
cmdgen.UdpTransportTarget((host, 161)),
(oid),
)
if errorIndication:
print (errorIndication)
else:
if errorStatus:
print ('%s at %s\n' % (
errorStatus.prettyPrint(),
errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
))
else:
for varBindTableRow in varBindTable:
for name, val in varBindTableRow:
result2 = {}
result2['OID']= str(val)
print(result2)
#return (name.prettyPrint(),val.prettyPrint())
#print ('%s = %s' % (name.prettyPrint(), val.prettyPrint()))
result = {}
result['test1']= SnmpGetNext(host,community,test1)
print(result)
期望的结果是通过运行
SnmpGetNext(主机、社区、测试1)
要获得:
1.3.6.1.2.1.2.2.1.14.1
1.3.6.1.2.1.2.2.1.14.2所以基本上我不得不使用而不是返回,屈服,现在它工作得很好。用你所有的
结果2
制作一个列表,并在最后返回列表。嗨,奥斯汀,谢谢,我尝试过了,在函数中使用它之前打印了列表,我有两个值,一旦我写下返回列表并尝试再次打印该列表,我只有一个值,知道吗?