Python 으로 snmp 로 접속하여 제어 하고 싶다면
Python 라이브러리 중 pysnmp 사용 방법을 간략하게 소개해보려고 한다.
from pysnmp.hlapi import *
HOST = "192.168.1.1" # 접속할 장비의 IP 입력할 것
PORT = 161
COMMUNITY = "aqXXlaKqjS" # 접속할 장비의 community 정보 입력할 것
engine = SnmpEngine()
host = UdpTransportTarget((HOST, PORT))
community = CommunityData(COMMUNITY, mpModel=1)
identity_obj_list = [
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0')),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0))
]
for identity_obj in identity_obj_list:
iterator = getCmd(engine, community, 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(' = '.join([x.prettyPrint() for x in varBind]))
SNMPv2-MIB::sysName.0 = kTLgIzt
SNMPv2-MIB::sysName.0 = kTLgIzt
from pysnmp.hlapi import *
HOST = "20.20.20.20"
PORT = 161
COMMUNITY = "aqXXlaKqjS"
engine = SnmpEngine()
host = UdpTransportTarget((HOST, PORT))
community = CommunityData(COMMUNITY, mpModel=1)
identity_obj_list = [
ObjectType(ObjectIdentity('1.3.6.1.2.1.1.5.0'), "SET_HOST_NAME_1"),
ObjectType(ObjectIdentity('SNMPv2-MIB', 'sysName', 0), "SET_HOST_NAME_2")
]
for identity_obj in identity_obj_list:
iterator = setCmd(engine, community, 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(' = '.join([x.prettyPrint() for x in varBind]))
SNMPv2-MIB::sysName.0 = SET_HOST_NAME_1
SNMPv2-MIB::sysName.0 = SET_HOST_NAME_2
Escape character is '^]'.
SET_HOST_NAME_2 login:
from pysnmp.hlapi import *
HOST = "20.20.20.20"
PORT = 161
COMMUNITY = "aqXXlaKqjS"
engine = SnmpEngine()
host = UdpTransportTarget((HOST, PORT))
community = CommunityData(COMMUNITY, mpModel=1)
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, community, 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 = kTLgIzt
[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