cisco开关启用端口pysnmp

时间:2016-09-05 08:47:29

标签: cisco pysnmp

尝试将此行转换为pysnmp

snmpset -v 2c -On -r 5 -t 2 -c private ip-address .1.3.6.1.2.1.2.2.1.7.369098771 i 1

我正在尝试使用work walk功能并对其进行修改但是我对SNMP的了解使得很难理解pysnmp doc

这只是代码的一部分

来自pysnmp.entity.rfc3413.oneliner import cmdgen

        device_target = (self.ip, self.port)
        res = None
        # Create a PYSNMP cmdgen object
        cmd_gen = cmdgen.CommandGenerator()
        (error_detected, error_status, error_index, snmp_data) = cmd_gen.setCmd(
            cmdgen.CommunityData(community_string),
            cmdgen.UdpTransportTarget(device_target), '.1.3.6.1.2.1.2.2.1.7.369098771', 1
            lookupNames=True, lookupValues=True)

我知道我错过了什么,请任何人帮忙

1 个答案:

答案 0 :(得分:1)

我强烈建议您将pysnmp升级到最新发布的版本并使用“hlapi”界面。

from pysnmp.hlapi import *

device_target = (self.ip, self.port)
community_string = 'private'

cmd_gen = setCmd(SnmpEngine(),
                 CommunityData(community_string),
                 UdpTransportTarget(device_target, timeout=2.0, retries=5),
                 ContextData(),
                 ObjectType(ObjectIdentity('1.3.6.1.2.1.2.2.1.7.369098771'), Integer32(1)),
                 lookupMib=False
)

res = None  # True denotes a failure

errorIndication, errorStatus, errorIndex, varBinds = next(cmd_gen)

if errorIndication:
    res = errorIndication
elif errorStatus:
    res = '%s at %s' % (errorStatus.prettyPrint(),
                        errorIndex and varBinds[int(errorIndex) - 1][0] or '?')

if res:
    print('SNMP failed: %s' % res)

参考文献:here