【问题标题】:capture plink failed connection messages to file [duplicate]将 plink 失败的连接消息捕获到文件
【发布时间】:2016-05-28 12:20:08
【问题描述】:

我在 windows 中使用 plink 连接到网络设备并将输出捕获到文件中。那部分效果很好。

这是我在批处理程序中输入的命令行示例。

plink.exe -v -l [username] -pw [password] [ip address] -m "c:\empty.txt" < "c:\commands.txt" > "c:\command_output.txt"

command_output.txt 仅包含 ssh 会话的输出。

我想添加一些错误处理,以防它无法连接。我不知道如何让连接消息转到文件。他们只进入命令窗口。我尝试使用另一个批处理程序中的 start /c cmd.exe 并将该输出重定向到另一个日志文件。尝试使用 2>&1。我什么都得不到。它总是只进入命令窗口。

这是我想要捕获的示例。

Looking up host "xxx.xxx.xxx.xxx"
Connecting to xxx.xxx.xxx.xxx port 22
Failed to connect to xxx.xxx.xxx.xxx: Network error: Connection timed out
Network error: Connection timed out
FATAL ERROR: Network error: Connection timed out

【问题讨论】:

  • 我让它工作了。只需要得到正确的组合。我将 plink 命令放在一个单独的批处理程序中,并使用“调用”从不同的批处理程序启动它,将输出重定向到一个文件,并且必须使用“2>&1”来包含 STDERR。有点杂乱无章,但它确实有效。
  • 您不需要第二个批处理文件。但请注意,重定向的顺序很重要。这是正确的:&gt; "c:\command_output.txt" 2&gt;&amp;1,但这不起作用:2&gt;&amp;1 &gt; "c:\command_output.txt"(因为它首先将错误输出重定向到默认的标准输出,此时控制台是什么)

标签: windows batch-file plink


【解决方案1】:

所以,总结一下使用下面的命令。我将 @Martin Prikryl 的建议合并到一个答案中,因此这显示为已回答。

plink.exe -v -l [username] -pw [password] [ip address] -m "c:\empty.txt" < "c:\commands.txt" > "c:\command_output.txt" 2>&1

【讨论】:

  • @Don,这对您有用吗?不需要额外的批处理文件?如果是这样,请考虑将答案标记为“已接受”。 Here's why 值得。
猜你喜欢
  • 2016-05-19
  • 2013-07-07
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
相关资源
最近更新 更多