bash 运行 ssh 并立即执行命令

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18522647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:07:59  来源:igfitidea点击:

Run ssh and immediately execute command

bashunixssh

提问by cadlac

I'm trying to find UNIX or bash command to run a command after connecting to an ssh server. For example:

我试图在连接到 ssh 服务器后找到 UNIX 或 bash 命令来运行命令。例如:

ssh name@ip "tmux list-sessions"

The above code works, it lists the sessions, but it then immediately disconnects. Putting it in the sshrc on the server side works, but I need to be able to type it in client side. I want to be able to run a command, it logs in, opens up the window, then runs the command I've set. Ive tried

上面的代码有效,它列出了会话,但随后立即断开连接。将它放在服务器端的 sshrc 中可以工作,但我需要能够在客户端输入它。我希望能够运行一个命令,它登录,打开窗口,然后运行我设置的命令。我试过了

[command] | ssh name@ip

ssh name@ip [command]

ssh name@ip "[command]"

ssh -t name@ip [command]

回答by Barmar

ssh -t 'command; bash -l'

will execute the command and then start up a login shell when it completes. For example:

将执行命令,然后在完成时启动登录 shell。例如:

ssh -t [email protected] 'cd /some/path; bash -l'

回答by gypaetus

You can use the LocalCommandcommand-line option if the PermitLocalCommandoption is enabled:

LocalCommand如果PermitLocalCommand启用了该选项,您可以使用命令行选项:

ssh username@hostname -o LocalCommand="tmux list-sessions"

For more details about the available options, see the ssh_configman page.

有关可用选项的更多详细信息,请参阅ssh_config手册页。

回答by Keith Thompson

This isn't quitewhat you're looking for, but I've found it useful in similar circumstances.

这不是你在寻找什么,但我发现它在类似情况下非常有用。

I recently added the following to my $HOME/.bashrc(something similar should be possible with shells other than bash):

我最近将以下内容添加到我的$HOME/.bashrc(使用 bash 以外的 shell 应该可以实现类似的东西):

if [ -f $HOME/.add-screen-to-history ] ; then
    history -s 'screen -dr'
fi

I keep a screensession running on one particular machine, and I've had problems with sshconnections to that machine being dropped, requiring me to re-run screen -drevery time I reconnect.

我让一个screen会话在一台特定的机器上运行,但我遇到了与ssh那台机器的连接断开的问题,screen -dr每次重新连接时都需要我重新运行。

With that addition, and after creating that (empty) file in my home directory, I automatically have the screen -drcommand in my history when my shell starts. After reconnecting, I can just type Control-PEnterand I'm back in my screen session -- or I can ignore it. It's flexible, but not quiteautomatic, and in your case it's easier than typing tmux list-sessions.

有了这个添加,并在我的主目录中创建了那个(空)文件之后,screen -dr当我的 shell 启动时,我的历史记录中会自动拥有该命令。重新连接后,我只需输入即可Control-PEnter返回屏幕会话——或者我可以忽略它。它很灵活,但不是自动,在您的情况下,它比键入tmux list-sessions.

You might want to make the history -scommand unconditional.

您可能希望使history -s命令无条件。

This does require updating your $HOME/.bashrcon each of the target systems, which might or might not make it unsuitable for your purposes.

这确实需要$HOME/.bashrc在每个目标系统上更新您的系统,这可能会或可能不会使其不适合您的目的。