通过ssh执行命令,然后运行bash。

5
我想设置一个脚本,打开终端,进行ssh连接到远程服务器,并执行一个命令(在我的情况下是tail -F logfile)。
我目前所拥有的是以下内容:
gnome-terminal -e 'ssh -t server "tail -F logfile"'

这在一定程度上是有效的。-t 确保像 SIGINT 这样的信号通过远程运行的命令发送。

然而,当我按下 ctrl-c 终止 tail 时,我真的希望能够进入远程服务器上的 bash 终端。现在,如果我按下 ctrl-c 终止 tail,那么 tail 就会关闭,导致 ssh 退出,从而关闭整个终端。

我想要的是终止 tail 并留下一个远程服务器上的 bash shell。

我尝试了以下操作:

gnome-terminal -e 'ssh -t server "tail -F logfile; /bin/bash"'

但这似乎不起作用。也就是说,如果我没有gnome-terminal直接运行ssh -t ...,则会看到以下内容:

some lines
from the log
^CConnection to server closed.

但是,如果我这样做

gnome-terminal -e 'ssh -t server "nonexistantcommand; /bin/bash"'

然后我收到一个错误,说找不到nonexistantcommand命令,然后我就会进入远程服务器上的bash...

有没有人对如何启动这个有任何建议或提示?提前感谢。

3个回答

2

这里有一个不怎么好的解决方案:

gnome-terminal -e 'ssh -t server "echo \"tail -F logfile;rm /tmp/foo\" > /tmp/foo; bash --rcfile /tmp/foo"'

这似乎有效。谢谢 :) chx在他的回答中有更多解释为什么。 - vmpstr

1
--init-file filename
--rcfile filename

在交互式 shell 中执行来自文件名(而不是 `~/.bashrc`)的命令。因此,使用 --rcfile 运行 bash,指向运行 tail -F 的脚本。

1

为什么不做显而易见的事情呢?“如果你收到了 SIGINT 信号,执行一个交互式 shell。”

gnome-terminal -e 'ssh -t server "trap \"exec sh -i\" INT; tail -F logfile"'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接