命令选型与 CLI

命令选型指南

需求推荐命令说明
立刻执行一条命令rauto exec最适合临时排障和快速验证
按 profile 执行 show 对象rauto show把 interfaces、route 等对象映射成真实命令
跨多条已保存连接执行解析巡检rauto show --target/--group/--label适合 inventory 级只读查询
拉取完整配置并生成哈希rauto config fetch适合配置备份与漂移检测
自动发现网段内 SSH 设备rauto device discover扫描、识别、筛选并保存连接
渲染已保存模板并执行rauto template适合把命令逻辑做成可复用模板
驱动交互式 prompt/responserauto flow适合向导式 CLI 操作
通过 SFTP 上传本地文件rauto upload要求目标主机支持 sftp 子系统
执行一个带回滚的事务块rauto tx适合单目标、单事务单元
按 JSON 执行事务工作流rauto tx-workflow适合拆成多个 block 的变更流程
执行多设备计划rauto orchestrate适合多台设备串行/并发编排
打开本地控制台rauto web适合自管理和可视化执行
作为托管执行节点启动rauto agent适合接入 rauto-manager

公共连接参数

大多数执行类命令都会复用同一组全局连接参数:

参数说明
--host设备主机名或 IP
--credential可复用设备凭证名称或稳定 ID
--ssh-portSSH 端口
--ssh-securitySSH 兼容档位:secure、balanced、legacy-compatible
--linux-shell-flavorLinux shell 退出码解析方式:posix 或 fish
--device-profile, -d目标设备 profile,默认是 autodetect
--force-autodetect忽略已缓存的自动探测结果,重新探测目标
--connection, -c直接引用已保存连接配置
--save-connection, -S成功连接后保存当前有效配置
--session-retries显式开启瞬时故障重试,默认值为 0
--retry-initial-backoff-ms / --retry-max-backoff-ms限制指数退避时间

执行模式可以写成有序候选列表,例如 --mode 'Enable,Config' 或 --mode 'Root|User'。rauto 会先按所选 Profile 校验候选值,再由 rneter 使用第一个可达模式。

最常用的命令示例

exec

rauto exec "show ip int br" \
  --host 192.168.1.1 \
  --credential network-admin \
  --device-profile cisco_ios

如果需要在指定模式执行:

rauto exec "show bgp neighbor" --connection core-sw-01 --mode Enable

exec、flow、show 和 config fetch 还支持可重复的 --target、--group、--label/--tag 选择器,并通过 --max-parallel 限制并发。

为原始命令输出启用 TextFSM 解析:

rauto exec "show version" \
  --connection core-sw-01 \
  --parse-textfsm

把解析结果导出到 Excel:

rauto exec "show version" \
  --connection core-sw-01 \
  --parse-textfsm \
  --textfsm-excel ./show-version.xlsx

--textfsm-excel 本身也会启用 TextFSM 解析,所以这个示例里的 --parse-textfsm 可以省略。

show

rauto show interfaces --connection core-sw-01

常用巡检参数:

rauto show --list --device-profile cisco_ios
rauto show route --connection core-sw-01 --print-command
rauto show interfaces --connection core-sw-01 --no-parse
rauto show interfaces --connection core-sw-01 --textfsm-strict-errors

跨 saved connection 批量执行 show:

rauto show interfaces \
  --target core-sw1 \
  --target core-sw2 \
  --group access \
  --label campus

rauto show route --group core --tag prod --textfsm-excel ./routes.xlsx

show 默认启用 TextFSM 解析。对象目录、Excel 导出和自定义映射请继续阅读 Show 对象与 TextFSM。

template

rauto template configure_vlan.j2 \
  --vars ./vars.json \
  --connection core-sw-01

只看渲染结果,不实际执行:

rauto template configure_vlan.j2 --vars ./vars.json --dry-run

flow

rauto flow \
  --template cisco_like_copy \
  --vars-json '{"command":"copy scp: flash:/new.bin","server_addr":"192.168.1.50","remote_path":"/images/new.bin","transfer_username":"backup","transfer_password":"secret"}' \
  --connection core-01

同一条 flow 也可以在去重后的多目标集合上执行:

rauto flow --template cisco_like_copy --group access --max-parallel 4

config fetch

rauto config fetch --group core --kind running --output-dir ./configs

命令目录覆盖、归一化与输出规则请查看配置拉取与漂移哈希。

device discover

rauto device discover 192.168.60.0/24 --credential network-admin

筛选、TUI 操作和安全导入请查看设备自动发现。

upload

rauto upload \
  --local-path ./daemon.conf \
  --remote-path /tmp/daemon.conf \
  --connection linux-jump-01

录制与回放

执行类命令普遍支持录制:

rauto exec "show version" \
  --connection core-sw-01 \
  --record-file ./recording.jsonl \
  --record-level key-events-only

回放时可以先列出录制事件:

rauto session replay ./recording.jsonl --list

也可以回放某条命令:

rauto session replay ./recording.jsonl --command "show version" --mode Enable

连接配置的推荐做法

临时命令阶段

先创建一次凭证,之后为直连目标传入 --host 和 --credential。

rauto credential add network-admin

进入重复执行阶段

改用已保存连接:

rauto connection add edge-01 \
  --host 192.168.1.11 \
  --credential network-admin \
  --device-profile huawei

之后统一使用:

rauto exec "display version" --connection edge-01

进入多设备阶段

配合 inventory group、tx-workflow 和 orchestrate 使用,会比纯手写连接参数更稳定。

完整的管理命令族

当前 CLI 源码定义的管理面要比前面的快速选型表更大。

rauto profile

根据当前源码,支持:

  • list
  • autodetect [-v|-vv]
  • show <name>
  • delete-custom <name>
  • copy-builtin <source> <name> [--overwrite]
  • diagnose <name> [--json]

rauto device / rauto connection

根据当前源码,支持:

  • test
  • list
  • show <name>
  • delete <name>
  • add <name>
  • import <file> [--json]
  • discover <targets...> [--port ...] [--probe-credential ...] [--auto-save]
  • discover list [--status ...] [--profile ...] [--port ...] [--search ...] [--json]
  • discover save [host[:port] ...] [--profile ...] [--overwrite]

rauto credential

当前凭证操作包括:

  • list [--json]
  • show <名称或 ID> [--json]
  • add <名称> [--login-username ...] [--enable] [--json]
  • update <名称或 ID> [--name ...] [--enable|--disable-enable] [--json]
  • import <文件> [--json]
  • delete <名称或 ID>

凭证密钥与连接元数据分离加密。完整生命周期可继续阅读设备凭证。

rauto inventory

当前 inventory 操作包括:

  • group list [--json]
  • group show <name> [--json]
  • group upsert <name> [--file <path> | --content <json>]
  • group delete <name>

Group 只包含名称、可选说明和已保存连接主机名;变量仍属于执行输入,不再是 inventory group 状态。

rauto session

当前支持:

  • list [connection-name] [--limit <n>] [--json]
  • show [id] [--json|--raw]
  • delete <id>
  • replay [record-file] [--id <id>] [--list] [--command <cmd>] [--mode <mode>]

rauto blacklist

当前支持:

  • list
  • add <pattern>
  • delete <pattern>
  • check <command>

rauto templates

当前支持:

  • list
  • show <name>
  • create <name> [--file <path> | --content <template>]
  • update <name> [--file <path> | --content <template>]
  • delete <name>

rauto flow-template

当前支持:

  • list
  • show <name>
  • create <name> [--file <path> | --content <toml>]
  • update <name> [--file <path> | --content <toml>]
  • delete <name>

rauto textfsm

根据当前源码,支持:

  • template list
  • template show <name>
  • template create <name> [--file <path> | --content <textfsm>]
  • template update <name> [--file <path> | --content <textfsm>]
  • template delete <name>
  • mapping list [--profile <profile>]
  • mapping set --profile <profile> --command <command> --template <name>
  • mapping delete --profile <profile> --command <command>

rauto show-object

根据当前源码,支持:

  • list [--profile <profile>]
  • set --profile <profile> --object <object> --command <command> [--mode <mode>] [--textfsm-template <name>] [--textfsm-mapping-command <command>] [--disabled]
  • delete --profile <profile> --object <object>

rauto tx-workflow template

当前保存的 workflow JSON 模板操作包括:

  • list
  • show <name>
  • create <name> [--file <path> | --content <json>]
  • update <name> [--file <path> | --content <json>]
  • delete <name>

rauto orchestrate template

当前保存的 orchestration JSON 模板操作包括:

  • list
  • show <name>
  • create <name> [--file <path> | --content <json>]
  • update <name> [--file <path> | --content <json>]
  • delete <name>

rauto backup

当前支持:

  • create [--output <archive>]
  • restore <archive> [--replace]
  • list

你可能还会用到的管理命令

  • rauto profile list
  • rauto profile autodetect -v
  • rauto profile show <name>
  • rauto templates list
  • rauto flow-template list
  • rauto textfsm template list
  • rauto show-object list --profile <name>
  • rauto connection list
  • rauto session list <connection-name>
  • rauto blacklist list
  • rauto backup list

录制与回放细节

从当前源码和 README 来看,录制并不是附带能力,而是第一等能力。

当前录制 / 回放相关参数包括:

  • exec/template/flow/upload/tx/tx-workflow --record-file <path>
  • exec/template/flow/upload/tx/tx-workflow --record-level <key-events-only|full>
  • session replay <record_file> --list
  • session replay <record_file> --command <cmd> [--mode <mode>]
  • session replay --id <record-id> --list

当前 README 还指出:

  • Linux shell 场景下,回放的 SessionEvent::CommandOutput 可能包含 exit_code
  • key-events-only 是最小审计级别
  • full 会包含更丰富的 prompt 与状态切换细节

企业级使用建议

把 CLI 当成命令族,而不是单条命令

在企业使用中,更适合把 rauto 理解成一整套命令族,而不是只会跑一条 exec。

典型运维路径通常是:

  1. 先用 connection add 规范目标
  2. 用 profile autodetect、profile show 或 profile diagnose 验证 profile 行为
  3. 用 show 做只读巡检和 TextFSM 解析
  4. 用 template 或 flow 执行可复用逻辑
  5. 用 tx / tx-workflow / orchestrate 做受控变更下发
  6. 用 session list/show/replay 与备份做审计和恢复

如果需要解析只读巡检结果,下一步请看 Show 对象与 TextFSM。