--- title: 命令行使用 description: 使用 SwitchOps CLI 执行全量或单设备巡检、ARP/MAC/端口查询和历史趋势查看。 updated: 2026-09-04 navigation: icon: lucide:square-terminal --- ## 命令结构 CLI 入口为 `cli.py`,包含三个主命令: ```text python cli.py inspect ... python cli.py query ... python cli.py trend ... ``` 查看内置帮助: ```bash python cli.py --help python cli.py inspect --help python cli.py query --help python cli.py trend --help ``` ## 执行巡检 ### 全量巡检 ```bash python cli.py inspect --config config/devices.yaml ``` 程序会加载所有设备,只巡检 `enabled: true` 的项目,并按照 `settings.yaml` 的并发和重试参数执行。 ### 按 IP 巡检单台设备 ```bash python cli.py inspect \ --config config/devices.yaml \ --target 192.168.1.1 ``` ### 按设备名称巡检 ```bash python cli.py inspect \ --config config/devices.yaml \ --target "核心交换机-S12704-3F-01" ``` `--target` 必须与配置中的 `host` 或 `name` 完全一致。没有匹配到启用设备时,程序会提示“没有可巡检的启用设备”。 ### 巡检参数 | 参数 | 是否必填 | 默认值 | 说明 | | --- | --- | --- | --- | | `--config` | 否 | `config/devices.yaml` | 设备清单路径。 | | `--target` | 否 | 空 | 按设备名称或 IP 精确筛选。 | 完成后终端会输出成功数、失败数、汇总报告路径,以及存在失败设备时的失败清单路径。 ## 查询 ARP ```bash python cli.py query arp \ --config config/devices.yaml \ --target 192.168.1.1 ``` 按关键字过滤: ```bash python cli.py query arp \ --config config/devices.yaml \ --target 192.168.1.1 \ --keyword 10.10.20.15 ``` ## 查询 MAC 地址表 ```bash python cli.py query mac \ --config config/devices.yaml \ --target 192.168.1.1 ``` 按 VLAN 或接口过滤: ```bash python cli.py query mac \ --config config/devices.yaml \ --target 192.168.1.1 \ --keyword Vlan10 ``` ## 查询接口 ```bash python cli.py query interface \ --config config/devices.yaml \ --target 192.168.1.1 \ --interface GigabitEthernet2/0/7 ``` 接口查询必须提供 `--interface`。端口名会替换到厂商命令模板的 `{interface}` 占位符中。 ### 查询参数 | 参数 | 是否必填 | 说明 | | --- | --- | --- | | `--config` | 否 | 设备清单路径,默认 `config/devices.yaml`。 | | `--target` | 是 | 设备名称或 IP,必须精确匹配启用设备。 | | `--keyword` | 否 | 对返回结果进行不区分大小写的本地逐行过滤。 | | `--interface` | 接口查询必填 | 完整接口名称。 | 所有查询都会把终端显示的结果保存到当天的 `outputs/YYYY-MM-DD/queries/` 目录。连接失败时也会保存一份包含友好错误信息的查询文件。 ## 查看历史趋势 先至少成功执行一次巡检,让程序生成 `outputs/history.db`。 查看最近 20 条记录: ```bash python cli.py trend ``` 只看某台设备: ```bash python cli.py trend --host 192.168.1.1 ``` 调整记录数量: ```bash python cli.py trend --host 192.168.1.1 --limit 50 ``` 读取其他数据库: ```bash python cli.py trend --db D:\SwitchOpsData\history.db --limit 100 ``` 趋势表显示:巡检时间、设备名称、IP、CPU、内存、接口 up/down 数量、结论和异常摘要。 ### 趋势参数 | 参数 | 默认值 | 说明 | | --- | --- | --- | | `--db` | `outputs/history.db` | SQLite 历史库路径。 | | `--host` | 空 | 按设备 IP 精确过滤。 | | `--limit` | `20` | 最多显示的记录数。 | ## 自动化调用建议 需要通过 Windows 任务计划程序或其他调度器定期运行时,可以调用: ```powershell Set-Location D:\SwitchOps .\.venv\Scripts\python.exe cli.py inspect --config config\devices.yaml ``` 调度账号必须具备读取配置、写入 `outputs` 和访问交换机管理网络的权限。定时任务上线前,应先手工执行相同命令验证工作目录和 Python 路径。