Python:网络交换机SNMP

示例(一)

# more snmp1.py 
#!/usr/bin/python3.4
# -*- coding=utf-8 -*-

from pysnmp.hlapi import *

#varBinds是列表,列表中的每个元素的类型是ObjectType(该类型的对象表示MIB variable)
errorIndication, errorStatus, errorindex, varBinds = next(
    getCmd(SnmpEngine(),
          CommunityData('CommunityString'),
          UdpTransportTarget(('交换机IP地址',161)),
          ContextData(),
          ObjectType(ObjectIdentity('1.3.6.1.2.1.1.1.0')),
          ObjectType(ObjectIdentity('1.3.6.1.2.1.1.2.0')))
)

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

for varBind in varBinds:
    print(varBind)

执行结果:

# python snmp1.py 
SNMPv2-MIB::sysDescr.0 = Dell Networking OS
Operating System Version: 2.0
Application Software Version: 9.11(2.1)
Series: S4048T-ON
Copyright (c) 1999-2017 by Dell Inc. All Rights Reserved.
Build Time: Mon Jun  5 09:25:23 2017
SNMPv2-MIB::sysObjectID.0 = SNMPv2-SMI::enterprises.6027.1.3.30
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注