from pysnmp.hlapi import UsmUserData, usmHMACSHAAuthProtocol, UdpTransportTarget, usmDESPrivProtocol, \
usmAesCfb128Protocol
from pysnmp.entity.rfc3413.oneliner import cmdgen
defget_oid_value(username, auth_key, priv_key, host, oid):
cg = cmdgen.CommandGenerator()
iterator = cg.getCmd(
UsmUserData(username, auth_key, priv_key,
usmHMACSHAAuthProtocol, usmAesCfb128Protocol),
UdpTransportTarget((host,161), timeout=10, retries=1),
oid)
errorIndication, errorStatus, errorIndex, varBinds = iterator
if errorIndication:# SNMP engine errorsprint("errorIndication", errorIndication)else:if errorStatus:# SNMP agent errorsprint('%s at %s'%(errorStatus.prettyPrint(), varBinds[int(errorIndex)-1]if errorIndex else'?'))else:for varBind in varBinds:# SNMP response contentsprint([x.prettyPrint()for x in varBind])print(varBinds[0][1])return varBinds[0][1]
无加密,简单版本
from pysnmp.entity.rfc3413.oneliner import cmdgen
cmdGen = cmdgen.CommandGenerator()
errorIndication, errorStatus, errorIndex, varBinds = cmdGen.getCmd(
cmdgen.CommunityData('public'),
cmdgen.UdpTransportTarget(('localhost',161)),'1.3.6.1.2.1.1.1.0','1.3.6.1.2.1.1.6.0')# Check for errors and print out resultsif errorIndication:print(errorIndication)else:if errorStatus:print('%s at %s'%(
errorStatus.prettyPrint(),
errorIndex and varBinds[int(errorIndex)-1]or'?'))else:for name, val in varBinds:print('%s = %s'%(name.prettyPrint(), val.prettyPrint()))