将及时命令传递给ssh [英] pipe timely commands to ssh
问题描述
我正在尝试将命令传递给已打开的SSH会话.这些命令将由脚本生成,分析结果,并根据该命令发送下一个命令.
I am trying to pipe commands to an opened SSH session. The commands will be generated by a script, analyzing the results, and sending the next commands in accordance.
我不想将所有命令放在远程主机上的脚本中,而只运行该脚本,因为我也对SSH进程的状态感兴趣:在本地发送命令可以测试"是否SSH连接是否有效,并从SSH进程中获取适当的返回码.
I do not want to put all the commands in a script on the remote host, and just run that script, because I am interested also in the status of the SSH process: sending locally the commands allow to "test" whether the SSH connection is alive or not, and get the appropriate return code from the SSH process.
我尝试了以下方式使用
$ mkfifo /tpm/commands
$ ssh -t remote </tmp/commands
从另一个术语来看:
$ echo "command" >> /tmp/commands
问题:SSH告诉我不会为stdin打开伪tty,并在命令"终止时立即关闭连接.
Problem: SSH tells me that no pseudo-tty will be opened for stdin, and closes the connection as soon as "command" terminates.
我尝试了另一种方法:
$ ssh -t remote <<EOF
$(echo "command"; while true; do sleep 10; echo "command"; done)
EOF
但是,直到达到EOF为止,所有内容都不会刷新到ssh(对于我来说,是永不).
But then, nothing is flushed to ssh until EOF is reached (in my case, never).
你们有没有解决办法?
推荐答案
在完成操作之前,请停止关闭/tmp/commands.当您关闭管道时,ssh停止读取管道.
Stop closing /tmp/commands before you're done with it. When you close the pipe, ssh stops reading from it.
exec 7> /tmp/commands. # open once
echo foo >&7 # write multiple times
echo bar >&7
exec 7>&- # close once
您还可以使用 ssh -tt
强制ssh在遥控器上打开tty.
You can additionally use ssh -tt
to force ssh to open a tty on the remote.
这篇关于将及时命令传递给ssh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!