脚本:执行 SSH 命令并保持 shell 打开,通过管道输出到文件
我想执行 ssh 命令并将输出通过管道传输到文件。
一般来说我会这样做:
ssh user@ip "command" >> /myfile
问题是,一旦执行命令,ssh 就会关闭连接,但是 - 我的命令通过后台的另一个程序将输出发送到 ssh 通道,因此我没有收到输出。
我怎样才能让 ssh 让我的 shell 保持打开状态?
干杯
斯文
I would like to execute a ssh command and pipe the output to a file.
In general I would do:
ssh user@ip "command" >> /myfile
the problem is that ssh close the connection once the command is executed, however - my command sends the output to the ssh channel via another programm in the background, therefore I am not receiving the output.
How can I treat ssh to leave my shell open?
cheers
sven
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的理解是
command
启动一些后台进程,稍后可能会将一些输出写入终端。如果command
在此之前终止,则 ssh 会话将终止,并且不会有终端可供后台程序写入。一个简单而幼稚的解决方案是睡眠足够长的时间
。比睡眠更好的解决方案是以某种更智能的方式等待后台进程完成,但如果没有更多细节,这是不可能说的。
My understanding is that
command
starts some background process that perhaps will write some output to the terminal later. Ifcommand
terminates before that the ssh session will be terminated and there will be no terminal for the background program to write to.One simple and naive solution is to just sleep long enough
A better solution than sleep would be to wait for the background process(es) to finish in some more intelligent way, but that is impossible to say without further details.
比 bash 更强大的是带有 Paramiko 和 PyExpect 的 Python。
Something more powerful than bash would be Python with Paramiko and PyExpect.