Run ssh and immediately execute command
This question already has an answer here:
ssh -t 'command; bash -l'
将执行该命令,然后在完成时启动登录shell。
You can use the LocalCommand
command-line option if the PermitLocalCommand
option is enabled:
ssh username@hostname -o LocalCommand="tmux list-sessions"
For more details about the available options, see the ssh_config
man page.
This isn't quite what 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):
if [ -f $HOME/.add-screen-to-history ] ; then
history -s 'screen -dr'
fi
I keep a screen
session running on one particular machine, and I've had problems with ssh
connections to that machine being dropped, requiring me to re-run screen -dr
every time I reconnect.
With that addition, and after creating that (empty) file in my home directory, I automatically have the screen -dr
command in my history when my shell starts. After reconnecting, I can just type Control-P Enter and I'm back in my screen session -- or I can ignore it. It's flexible, but not quite automatic, and in your case it's easier than typing tmux list-sessions
.
You might want to make the history -s
command unconditional.
This does require updating your $HOME/.bashrc
on each of the target systems, which might or might not make it unsuitable for your purposes.
上一篇: 如何使用ssh远程运行脚本文件
下一篇: 运行ssh并立即执行命令