pysnmp snmpv2 글에 이어서
이번 포스팅은 snmpv3 관련 예제를 간략히 소개하려고 합니다.
from pysnmp.hlapi import *
HOST = "20.20.20.20"
PORT = 161
USER = "LHZwj"
AUTH_KEY = "YEexkuDIkI304"
AUTH_PROTO = usmHMACMD5AuthProtocol
engine = SnmpEngine()
host = UdpTransportTarget((HOST, PORT))
user_data = UsmUserData(USER, AUTH_KEY, authProtocol=AUTH_PROTO)
cmd_list = [
(getCmd, ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0'))),
(setCmd, ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0'), "SET_HOST_NAME_1")),
(getCmd, ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0))),
(setCmd, ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), "SET_HOST_NAME_2")),
(getCmd, ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0)))
]
for cmd_func, identity_obj in cmd_list:
iterator = cmd_func(engine, user_data, host, ContextData(), identity_obj)
errorIndication, errorStatus, errorIndex, varBinds = next(iterator)
if errorIndication: # SNMP engine errors
print(errorIndication)
else:
if errorStatus: # SNMP agent errors
print('%s at %s' % (errorStatus.prettyPrint(),
varBinds[int(errorIndex)-1] if errorIndex else '?'))
else:
for varBind in varBinds: # SNMP response contents
print("[{}] ".format(cmd_func.__name__) +\
' = '.join([x.prettyPrint() for x in varBind]))
[getCmd] SNMPv2-MIB::sysName.0 = V8102_02
[setCmd] SNMPv2-MIB::sysName.0 = SET_HOST_NAME_1
[getCmd] SNMPv2-MIB::sysName.0 = SET_HOST_NAME_1
[setCmd] SNMPv2-MIB::sysName.0 = SET_HOST_NAME_2
[getCmd] SNMPv2-MIB::sysName.0 = SET_HOST_NAME_2