You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows上用snmpsim-lextudio配置多IP多进程SNMP v1/v2c代理

批量部署snmpsim-lextudio多SNMP代理方案(Windows环境)

一、前置准备:配置多本地IP

先在Windows网卡的IPv4设置中添加多个静态IP(比如127.0.0.1/24段的多个地址,或实际业务网段IP),确保每个代理能绑定到独立IP地址。

二、批量启动代理(解决数百个代理启动效率问题)

1. 批处理脚本方案(简单直接)

创建start_snmp_proxies.bat脚本,一键批量启动独立进程的代理:

@echo off
:: 定义待绑定的IP列表,可扩展到数百个
set "ips=127.0.0.1 127.0.0.2 127.0.0.3 127.0.0.4"
:: 基础数据目录,每个IP对应子目录
set "base_data_dir=.\data"

for %%i in (%ips%) do (
    :: 创建代理专属数据目录(不存在则新建)
    if not exist "%base_data_dir%\%%i" mkdir "%base_data_dir%\%%i"
    :: 复制基础模板数据文件到代理目录(如需统一模板)
    if not exist "%base_data_dir%\%%i\data.snmprec" copy "%base_data_dir%\base_template.snmprec" "%base_data_dir%\%%i\data.snmprec"
    :: 启动独立窗口的代理进程,禁用v3、指定社区字符串
    start "SNMP Proxy %%i" snmpsim-command-responder-lite --agent-udpv4-endpoint=%%i:161 --data-dir="%base_data_dir%\%%i" --community=public --no-v3
)
  • --no-v3:禁用SNMPv3,满足仅用v1/v2c的要求
  • --community=public:指定统一社区字符串
  • 每个代理绑定独立IP、监听161端口,且在单独窗口运行

2. Python脚本方案(大规模部署更灵活)

创建start_snmp_proxies.py,支持自动生成IP列表、动态修改数据文件:

import subprocess
import os

# 配置参数
BASE_DATA_DIR = ".\\data"
BASE_TEMPLATE = os.path.join(BASE_DATA_DIR, "base_template.snmprec")
# 自动生成100个本地IP,可按需调整数量
IP_LIST = [f"127.0.0.{i}" for i in range(1, 101)]
COMMUNITY = "public"
PORT = 161

# 确保基础目录存在
os.makedirs(BASE_DATA_DIR, exist_ok=True)

for ip in IP_LIST:
    # 用下划线替换IP的点作为目录名,避免路径问题
    proxy_data_dir = os.path.join(BASE_DATA_DIR, ip.replace(".", "_"))
    os.makedirs(proxy_data_dir, exist_ok=True)
    
    proxy_data_file = os.path.join(proxy_data_dir, "data.snmprec")
    # 复制模板文件并修改唯一固定值(如sysName、sysLocation)
    if not os.path.exists(proxy_data_file):
        with open(BASE_TEMPLATE, "rb") as src, open(proxy_data_file, "wb") as dst:
            dst.write(src.read())
        # 替换OID对应的值,实现每个代理的唯一标识
        with open(proxy_data_file, "r+", encoding="utf-8") as f:
            content = f.read()
            content = content.replace('1.3.6.1.2.1.1.5.0|4|SNMP-Server', f'1.3.6.1.2.1.1.5.0|4|SNMP-Server-{ip}')
            content = content.replace('1.3.6.1.2.1.1.6.0|4|Default-Location', f'1.3.6.1.2.1.1.6.0|4|Location-{ip}')
            f.seek(0)
            f.write(content)
            f.truncate()
    
    # 启动独立窗口的代理进程
    cmd = f'start "SNMP Proxy {ip}" snmpsim-command-responder-lite --agent-udpv4-endpoint={ip}:{PORT} --data-dir="{proxy_data_dir}" --community={COMMUNITY} --no-v3'
    subprocess.Popen(cmd, shell=True)

print(f"已启动 {len(IP_LIST)} 个SNMP代理进程")

三、实现动态递增对象的持久化

通过自定义snmpsim动态模块,处理可变值并在代理停止时保存数据:

1. 创建动态值处理脚本

在每个代理的数据目录中创建custom_dynamic.py:

from snmpsim.grammar import abstract
from snmpsim.record import dump
import os

# 持久化文件,绑定代理IP作为唯一标识
PERSIST_FILE = os.path.join(os.path.dirname(__file__), "counter_persist.dat")

class CounterRecord(abstract.AbstractRecord):
    grammar = dump.DumpGrammar()
    
    def __init__(self, oid, value, **context):
        self.oid = oid
        # 从持久化文件读取初始值,无则设为0
        self.counter = int(open(PERSIST_FILE).read().strip()) if os.path.exists(PERSIST_FILE) else 0
        super().__init__(oid, str(self.counter), **context)
    
    def evaluate(self, context=None):
        # 每次请求递增计数器并持久化
        self.counter += 1
        with open(PERSIST_FILE, "w") as f:
            f.write(str(self.counter))
        return str(self.counter), self.dataType

2. 修改数据文件引用动态模块

在代理的data.snmprec中,将需要动态递增的OID替换为:

1.3.6.1.2.1.1.3.0|6|@CounterRecord
  • |6|表示Counter32数据类型
  • @CounterRecord引用自定义的动态处理类

3. 启动时加载动态模块

修改启动命令,添加加载模块的参数:

snmpsim-command-responder-lite --agent-udpv4-endpoint=127.0.0.1:161 --data-dir=.\data\127_0_0_1 --community=public --no-v3 --v2c-mib-modules=custom_dynamic

批量脚本中直接将该参数加入启动命令即可。

四、批量管理代理

1. 批量停止代理

创建stop_snmp_proxies.bat:

@echo off
taskkill /IM snmpsim-command-responder-lite.exe /F

2. 监控代理状态

用netstat检查端口绑定情况:

netstat -ano | findstr ":161"

内容的提问来源于stack exchange,提问作者Ameen Nihad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 05:35:53