参考链接 【测试】python 模拟snmp-server 例子_Dalon 技术博客的技术博客_51CTO博客
本文转为python3代码,主要涉及改动:bytes类型的增减
snmp_sim_v2c.py
# -*- coding: utf-8 -*-
import binascii
import struct
import socket
import time
from threading import Thread
'''
windows OID
1.3.6.1.2.1.25.2.3.1.6.1 [ObjectIdentifier] //硬盘
1.3.6.1.2.1.25.2.3.1.6.2 [ObjectIdentifier] //硬盘
1.3.6.1.2.1.25.2.3.1.6.3 [ObjectIdentifier] //硬盘
1.3.6.1.2.1.25.2.3.1.6.4 [ObjectIdentifier] //硬盘
1.3.6.1.2.1.25.2.3.1.6.5 [ObjectIdentifier] //光盘
1.3.6.1.2.1.25.2.3.1.6.6 [ObjectIdentifier] //光盘
1.3.6.1.2.1.25.2.3.1.6.7 [ObjectIdentifier] //虚拟内存
1.3.6.1.2.1.25.2.3.1.6.8 [ObjectIdentifier] //物理内存
OID号不固定,当只有一块硬盘,一个光驱时,物理内存占用OID为1.2.5(2.1占用一般0,不是真实硬盘,不知道是什么意思,真正硬盘占用从2.2开始)
'''
# a 是一个真实的请求内容,def test_parse()函数可以将他解析出来,并打印出密码,请求类型,请求ID,OID对象
a = '''0x30, 0x82, 0x01, 0x09, 0x02, 0x01, 0x01, 0x04,
0x06, 0x70, 0x75, 0x62, 0x6c, 0x69, 0x63, 0xa0,
0x81, 0xfb, 0x02, 0x04, 0x4a, 0xbb, 0x2b, 0xac,
0x02, 0x01, 0x00, 0x02, 0x01, 0x00, 0x30, 0x81,
0xec, 0x30, 0x0d, 0x06, 0x09, 0x2b, 0x06, 0x01,
0x02, 0x01, 0x19, 0x02, 0x02, 0x00, 0x05, 0x00,
0x30, 0x0f, 0x06, 0x0b, 0x2b, 0x06, 0x01, 0x02,
0x01, 0x19, 0x03, 0x03, 0x01, 0x02, 0x01, 0x05,
0x00, 0x30, 0x0f, 0x06, 0x0b, 0x2b, 0x06, 0x01,
0x02, 0x01, 0x19, 0x02, 0x03, 0x01, 0x05, 0x01,
0x05, 0x00, 0x30, 0x0f, 0x06, 0x0b, 0x2b, 0x06,
0x01, 0x02, 0x01, 0x19, 0x02, 0x03, 0x01, 0x05,
0x02, 0x05, 0x00, 0x30, 0x0f, 0x06, 0x0b, 0x2b,
0x06, 0x01, 0x02, 0x01, 0x19, 0x02, 0x03, 0x01,
0x05, 0x03, 0x05, 0x00, 0x30, 0x0f, 0x06, 0x0b,
0x2b, 0x06, 0x01, 0x02, 0x01, 0x19, 0x02, 0x03,
0x01, 0x05, 0x04, 0x05, 0x00, 0x30, 0x0f, 0x06,
0x0b, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x19, 0x02,
0x03, 0x01, 0x06, 0x01, 0x05, 0x00, 0x30, 0x0f,
0x06, 0x0b, 0x2b, 0x06, 0x01, 0x02, 0x01, 0x19,
0x02, 0x03, 0x01, 0x06, 0x02, 0x05, 0x00, 0x30,
0x0f, 0x06, 0x0b, 0x2b, 0x06, 0x01, 0x02, 0x01,
0x19, 0x02, 0x03, 0x01, 0x06, 0x03, 0x05, 0x00,
0x30, 0x0f, 0x06, 0x0b, 0x2b, 0x06, 0x01, 0x02,
0x01, 0x19, 0x02, 0x03, 0x01, 0x06, 0x04, 0x05,
0x00, 0x30, 0x0f, 0x06, 0x0b, 0x2b, 0x06, 0x01,
0x02, 0x01, 0x19, 0x02, 0x03, 0x01, 0x06, 0x05,
0x05, 0x00, 0x30, 0x0f, 0x06, 0x0b, 0x2b, 0x06,
0x01, 0x02, 0x01, 0x19, 0x02, 0x03, 0x01, 0x06,
0x06, 0x05, 0x00, 0x30, 0x0f, 0x06, 0x0b, 0x2b,
0x06, 0x01, 0x02, 0x01, 0x19, 0x02, 0x03, 0x01,
0x06, 0x07, 0x05, 0x00, 0x30, 0x0f, 0x06, 0x0b,
0x2b, 0x06, 0x01, 0x02, 0x01, 0x19, 0x02, 0x03,
0x01, 0x06, 0x08, 0x05, 0x00'''
class SnmpReqParse:
struct_type = {0x02: 'INTEGER_TYPE',
0x30: 'SQUENCE_TYPE',
0x04: 'OCTET_TYPE',
0X05: 'NULL_TYPE',
0x06: 'OBJID_TYPE',
0xa0: 'GET_REQ_TYPE',
0xa5: 'GET_BULKREQ_TYPE',
0Xa2: 'RESPONSE_TYPE'}
def __init__(self, request):
self.request = request
self.objidList = []
self.requestID = None
self.getReqType = 0xa0
self.publicPasswd = ''
self.parse()
def parse(self):
current_pos = self.parse_header1()
if not current_pos:
print("parse SNMP header failed!")
return
current_pos = self.parse_header2(current_pos)
if not current_pos:
print("parse SNMP header failed!")
return
self.parse_obj(current_pos)
def parse_obj(self, pos):
# 开始解析obj对象了
current_pos = pos
stype = struct.unpack('B', self.request[current_pos:current_pos + 1])[0]
current_pos += 1
current_pos = self.struct_parse(current_pos)[0]
while len(self.request[current_pos:]) != 0:
stype = struct.unpack('B', self.request[current_pos:current_pos + 1])[0]
current