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

Web Excel下拉菜单触发SNMP命令控制交换机开关方案咨询

可行实现方案

1. Office Scripts + Power Automate(官方推荐,适配Web Excel)

Web Excel不支持VBA,但支持Office Scripts(需商业/教育版Office 365账号),搭配Power Automate可实现完整的触发-执行流程:

步骤1:创建ON/OFF下拉菜单

选中目标单元格区域,点击「数据」→「数据验证」,选择「序列」,输入ON,OFF作为来源,确认后单元格就会出现下拉选项。

步骤2:编写Office Script监听值变化

打开「自动化」选项卡→「新建脚本」,编写以下逻辑(根据你的交换机SNMP配置调整OID和命令):

function main(workbook: ExcelScript.Workbook) {
  // 假设下拉单元格在B列,A列存交换机IP
  const targetCell = workbook.getActiveCell();
  const status = targetCell.getValue() as string;
  const switchIP = targetCell.offset(0, -1).getValue() as string;

  let snmpCmd = "";
  if (status === "ON") {
    // 替换为你的交换机开启电源的SNMP命令(比如snmpset)
    snmpCmd = `snmpset -v 2c -c your_community ${switchIP} 1.3.6.1.4.1.xxxx.xxxx.1.0 i 1`;
  } else if (status === "OFF") {
    // 替换为关闭电源的SNMP命令
    snmpCmd = `snmpset -v 2c -c your_community ${switchIP} 1.3.6.1.4.1.xxxx.xxxx.1.0 i 0`;
  }

  // 将命令和IP传递给Power Automate
  return { command: snmpCmd, ip: switchIP };
}

写完后保存脚本,在编辑器中设置触发器为「当单元格值变化时」,指定监控的下拉单元格区域。

步骤3:用Power Automate执行SNMP命令

Office Scripts无法直接执行系统命令,需要Power Automate中转:

  • 新建Power Automate流,触发条件选「Excel Online (Business) - 当单元格值变化时」,关联你的Web表格和监控区域。
  • 添加「运行脚本」步骤,调用刚才的Office Script,获取SNMP命令和IP。
  • 若要在本地执行命令:添加「桌面流」步骤,调用本地的SNMP工具(如net-snmp的snmpset);若用云SNMP服务,直接调用服务API传递参数。

2. 轻量替代方案(适合无Office 365商业版权限)

如果没有Office Scripts权限,可以用「下拉菜单+外部脚本定时检测」的方式:

步骤1:保留下拉菜单

按步骤1设置好ON/OFF下拉选项,同时在相邻单元格用公式生成对应SNMP命令,比如:

=IF(B2="ON","snmpset -v 2c -c your_community "&A2&" 1.3.6.1.4.1.xxxx.xxxx.1.0 i 1","snmpset -v 2c -c your_community "&A2&" 1.3.6.1.4.1.xxxx.xxxx.1.0 i 0")

(A2是交换机IP,B2是下拉单元格)

步骤2:编写外部脚本定时读取并执行

用Python/批处理脚本定时读取Web Excel的内容(可以先导出为本地文件,或用Excel API直接读取),检测到状态变化时执行SNMP命令。示例Python片段:

import openpyxl
import subprocess
import time

while True:
    # 加载导出的Excel文件(或用Microsoft Graph API读取在线表格)
    wb = openpyxl.load_workbook("switch_monitor.xlsx")
    ws = wb.active

    # 遍历交换机行(假设从第2行开始)
    for row in range(2, ws.max_row + 1):
        status = ws[f"B{row}"].value
        switch_ip = ws[f"A{row}"].value
        if status == "ON":
            subprocess.run(["snmpset", "-v", "2c", "-c", "your_community", switch_ip, "1.3.6.1.4.1.xxxx.xxxx.1.0", "i", "1"])
        elif status == "OFF":
            subprocess.run(["snmpset", "-v", "2c", "-c", "your_community", switch_ip, "1.3.6.1.4.1.xxxx.xxxx.1.0", "i", "0"])
    time.sleep(60)  # 每分钟检测一次

用Windows任务计划或Linux cron定时运行脚本即可。

关键注意事项

  • 确认交换机已开启SNMP服务,配置正确的社区字符串,且控制电源的OID符合厂商MIB文档(不同品牌交换机OID不同)。
  • 敏感场景建议使用SNMPv3加密传输,避免用默认的public社区字符串。
  • Power Automate桌面流需要在安装了SNMP工具的机器上运行,确保工具路径正确。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 03:37:38