batch-file - 使用 Plink 和批处理文件的多个命令用于 Cisco 交换机

标签 batch-file ssh putty cisco plink

我目前正在尝试编写一个脚本来更改 200 多台 Cisco 交换机的位置 (snmp)。

我的问题是我一次不能运行多个命令。我制作了一个批处理文件,它自动连接到交换机并读取列出命令的 .txt 文件。但无论我做什么,我得到的最好结果都是只执行了第一个命令。

批处理文件:

cmd.exe /c echo n | "Filepath(plink)" -ssh Switch Hostname -l Username -pw "Password" -m "txt File"

txt 文件:
conf t
snmp-server location test
end
wr
exit

我已经尝试过 txt 文件中的其他分隔符,例如 ; |等等
但似乎没有任何效果。

最佳答案

这实际上是 Cisco 的一个已知限制,它不支持 SSH“exec” channel 命令中的多个命令。

报价栏3.8.3.6 -m : read a remote command or script from a file PuTTY/Plink 手册:

With some servers (particularly Unix systems), you can even put multiple lines in this file and execute more than one command in sequence, or a whole shell script; but this is arguably an abuse, and cannot be expected to work on all servers. In particular, it is known not to work with certain ‘embedded’ servers, such as Cisco routers.



尽管实际上,您的命令可能不是独立的顶级 shell 命令。我猜 snmp-server (和其他)是 conf t 的子命令,不是吗?因此,即使 Cisco 确实支持“exec” channel 上的多个命令,您的代码也无法工作。

详情见How to type commands in PuTTY by creating batch file?

您需要执行 conf t然后将其子命令提供给其标准输入。

一种方法是这样的:
(
    echo snmp-server location test
    echo end
    echo wr
    echo exit
) | plink -ssh hostname -l username -pw password conf t

关于batch-file - 使用 Plink 和批处理文件的多个命令用于 Cisco 交换机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59661478/

相关文章:

Windows 批处理 : How can I ran multiple aliases in single line

windows - 批量测试变量是否等于空格

c# - 在 Process 中调用 robocopy 的批处理脚本不会终止

linux - scp 输出为日志文件

windows - Plink 工作目录

windows - 如何将 Haskell 嵌入到批处理文件中(hash-bang runhaskell for Windows)?

mysql - BASH 脚本 - 通过 ssh 连接并运行 mysql 命令

git - 如何创建存储库并将代码从服务器/cpanel 推送到 bitbucket

batch-file - 如何使用 PuTTY 批处理文件将上次修改的文件从远程服务器下载到本地

serial-port - PuTTY 串行连接重新连接