脚本:执行 SSH 命令并保持 shell 打开,通过管道输出到文件

发布于 2024-08-19 15:37:37 字数 232 浏览 3 评论 0原文

我想执行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花开雨落又逢春i 2024-08-26 15:37:37

我的理解是 command 启动一些后台进程,稍后可能会将一些输出写入终端。如果 command 在此之前终止,则 ssh 会话将终止,并且不会有终端可供后台程序写入。

一个简单而幼稚的解决方案是睡眠足够长的时间

ssh user@ip "command; sleep 30m" >> /myfile

。比睡眠更好的解决方案是以某种更智能的方式等待后台进程完成,但如果没有更多细节,这是不可能说的。

My understanding is that command starts some background process that perhaps will write some output to the terminal later. If command 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

ssh user@ip "command; sleep 30m" >> /myfile

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.

涫野音 2024-08-26 15:37:37

比 bash 更强大的是带有 Paramiko 和 PyExpect 的 Python。

Something more powerful than bash would be Python with Paramiko and PyExpect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文