gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:28:26 25 4
gpt4 key购买 nike

我目前正在尝试编写一个脚本来更改 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com