Windows批处理脚本:带有Plink的SSH在输出中显示奇怪的序列

时间:2015-11-24 21:29:23

标签: ssh plink

我正在编写一些命令行操作脚本,用于从Palo Alto 5060防火墙收集和解析特定的网络指标。我正在使用Plink和Windows批处理脚本。

@echo off
"C:\path\to\plink.exe" -ssh user@1.2.3.4 -pw password < "C:\path\to\commands.txt >> "C:\path\to\output.txt"

目前commands.txt的内容很简单。

show interface ethernet1/1

我无法让这个工作。我的output.txt有以下结果:

Last login: Tue Nov 24 15:43:13 2015 from localhost

show interface ethernet1/1Welcome user.
user@pa5060> show 
[Kuser@pa5060> show interface 
[Kuser@pa5060> show interface ethernet1/1

这不是正确的输出,命令的输入让我感到困惑。有没有人见过这样的东西?如果相关,则在此设备上有一个登录横幅。

1 个答案:

答案 0 :(得分:1)

我猜你在command.txt的末尾错过了一个新行,所以命令没有提交。

重复提示和[K序列:
这只是因为远程端需要一个交互式终端,并发送ANSI escape sequences来打印输出。

每一行都可能以CR(回车)字符开头,这会导致交互式终端覆盖前一行。但是,当您将输出重定向到文件时,这不起作用。虽然如果使用cmd.exe在终端(type output)上打印文件,您可能只会获得最后一行。

要使Plink不启用交互式终端,请使用-T command-line switch

"C:\path\to\plink.exe" -ssh user@1.2.3.4 -pw password -T < "C:\path\to\commands.txt >> "C:\path\to\output.txt"

虽然更好的是在PLink命令行上指定命令

"C:\path\to\plink.exe" -ssh user@1.2.3.4 -pw password show interface ethernet1/1 >> "C:\path\to\output.txt"

或使用-m switch

"C:\path\to\plink.exe" -ssh user@1.2.3.4 -pw password -m "C:\path\to\commands.txt >> "C:\path\to\output.txt"

不同之处在于,以这种方式指定的命令会在非交互式终端中自动执行,主要以更加受控的方式在“exec”通道中执行,然后在重定向输入时使用的“shell”通道中执行。所以你摆脱了“上次登录:”消息以及命令提示符(user@pa5060>)等。

相关问题