python pysnmp 實現snmpv3和snmpv2的getcmd

snmpv2的
python

from pysnmp.hlapi import *

iterator = getCmd(
    SnmpEngine(),
    CommunityData('public', mpModel=0),#V2的團體字
    UdpTransportTarget(('192.168.56.11', 161)),#目標IP和端口
    ContextData(),
    ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'))#查詢單個OID
    # ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))#經過OID名稱查詢
)

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

if errorIndication:
    print(errorIndication)

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

else:
    for varBind in varBinds:
        print(' = '.join([x.prettyPrint() for x in varBind]))

SNMPv3的
api

SNMPv3支持認證方式和加密以下:ide

     #usmHMACMD5AuthProtocol - MD5 hashing加密

    #usmHMACSHAAuthProtocol - SHA hashingip

    #usmNoAuthProtocol - no authenticationget

    #usmDESPrivProtocol - DES encryptionhash

    #usm3DESEDEPrivProtocol - triple-DES encryptionit

    #usmAesCfb128Protocol - AES encryption, 128-bitio

    #usmAesCfb192Protocol - AES encryption, 192-bitclass

    #usmAesCfb256Protocol - AES encryption, 256-bit

    #usmNoPrivProtocol - no encryption

from pysnmp.hlapi import *

iterator = getCmd(
    SnmpEngine(),
    UsmUserData('snmpuser', '12345678', '12345678',#第一個爲snmp用戶名,第二個爲認證密碼,第三個爲加密密碼
                authProtocol=usmHMACSHAAuthProtocol,#認證方式
                privProtocol=usmDESPrivProtocol),#認證密碼
    UdpTransportTarget(('192.168.56.11', 161)),
    ContextData(),
    ObjectType(ObjectIdentity('1.3.6.1.2.1.1.3.0'))
    # ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysDescr', 0))
)

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

if errorIndication:
    print(errorIndication)

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

else:
    for varBind in varBinds:
        print(' = '.join([x.prettyPrint() for x in varBind]))
相關文章
相關標籤/搜索