snmpv2的
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的
SNMPv3支持认证方式和加密如下:
#usmHMACMD5AuthProtocol - MD5 hashing
#usmHMACSHAAuthProtocol - SHA hashing
#usmNoAuthProtocol - no authentication
#usmDESPrivProtocol - DES encryption
#usm3DESEDEPrivProtocol - triple-DES encryption
#usmAesCfb128Protocol - AES encryption, 128-bit
#usmAesCfb192Protocol - AES encryption, 192-bit
#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]))
Ctrl+Enter 发布
发布
取消