pysnmp snmpv2

dingdong·2022년 1월 21일
0

pysnmp

목록 보기
1/2
post-custom-banner

Python 으로 snmp 로 접속하여 제어 하고 싶다면
Python 라이브러리 중 pysnmp 사용 방법을 간략하게 소개해보려고 한다.

pysnmp github



Get 예제

Get 예제 코드

  • '1.3.6.1.2.1.1.5.0'
    • 장비의 HOST명을 가져오라는 SNMP OID
  • ('SNMPv2-MIB', 'sysName', 0)
    • 장비의 HOST명을 가져오라는 MID

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


Set 예제

Set 예제 코드


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

HOST 로 접속하여 확인

  • HOST Name 이 변경된 것을 확인
  • 반드시 HOST에는 SNMP 서비스가 실행되어 있어야지 접속가능함
    • HOST 의 SNMP 정보가 일치해야됨.
Escape character is '^]'.

SET_HOST_NAME_2 login:


Get + Set 예제

Get + Set 예제 코드


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
profile
자동화 개발
post-custom-banner

0개의 댓글