如何让我的pysnmp脚本工作

时间:2013-08-14 05:48:53

标签: python snmp pysnmp

我正在尝试执行snmpwalk以获取设备每个接口上的错误数量(通过DNS名称)。

我可以从Debian框中成功运行以下snmpwalk

snmpwalk -v2c -c public atlanta-r1 1.3.6.1.2.1.2.2.1.14

结果:

IF-MIB::ifInErrors.1 = Counter32: 0<br>
IF-MIB::ifInErrors.2 = Counter32: 0<br>
IF-MIB::ifInErrors.3 = Counter32: 0<br>
....

我正在尝试使用pysnmp将其转换为Python脚本。我在使用它时遇到了一些麻烦。当我运行它时,我一直拒绝获得许可。任何人都可以帮我解决我的代码吗?感谢

from pysnmp.entity.rfc3413.oneliner import cmdgen

cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBindTable = cmdGen.nextCmd(
        cmdgen.CommunityData('public'),
        cmdgen.UdpTransportTarget(('atlanta-r1', 161)),
        '1.3.6.1.2.1.2.2.1.14',
)

if errorIndication:
    print(errorIndication)
else:
    if errorStatus:
        print('%s at %s' % (
            errorStatus.prettyPrint(),
            errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
            )
        )
    else:
        for varBindTableRow in varBindTable:
            for name, val in varBindTableRow:
                print('%s = %s' % (name.prettyPrint(), val.prettyPrint()))

1 个答案:

答案 0 :(得分:-1)

此源代码正确,并且可以通过替换主机名在我的系统上运行。尝试以sudo / administrator权限执行此脚本。

相关问题