With ssh I can do this:
ssh REMOTE -t 'tmux a || tmux'
With mosh the best I can do is this:
mosh REMOTE -- tmux a
But this won't work:
mosh REMOTE -- tmux a || tmux
Neither does this: (It doesn't matter whether it's single quote or double quote, I tried both)
mosh REMOTE -- 'tmux a || tmux'
So, my question is: How am I supposed to do this job?
Mosh will use the first available UDP port, starting at 60001 and stopping at 60999. If you are only going to have a small handful of concurrent sessions on a server, then you can forward a smaller range of ports (e.g., 60000 to 60010).
In computing, Mosh (mobile shell) is a tool used to connect from a client computer to a server over the Internet, to run a remote terminal. Mosh is similar to SSH, with additional features meant to improve usability for mobile users.
How Secure Is Mosh? Mosh makes the initial connection over SSH, so the authentication is about as secure as SSH is. It uses AES-128 encryption for traffic sent over UDP, so your traffic can't be sniffed. The main issue with Mosh is that it requires a lot of ports to be open.
Well, it seems that I have to explicitly use a shell to execute command:
mosh REMOTE -- sh -c 'tmux a || tmux'
EDIT
Instead of doing tmux a || tmux
, a better way is add new-session
to ~/.tmux.conf
and just run tmux
. That would make things much easier. I can do things like this now:
mosh REMOTE -- tmux
Awesome!
There might be more complicated commands than the examples given above. I wanted to make a command that reattaches to an existing tmux session if one exists but is not already attached, or a new one if there are none available.
Looking at this example, I would have done something like this:
function tmosh() {
mosh $1 -- (tmux ls | grep -vq attached && tmux at -t $( tmux ls | grep -vm1 attached | cut -d: -f1 ) ) || tmux new
}
But that doesn't work, per the original question above.
My solution so far is to have a wrapper script on the host servers:
tmux-reattach-if-exists
which consists simply of:
(tmux ls | grep -vq attached && tmux at -t $( tmux ls | grep -vm1 attached | cut -d: -f1 )) || tmux new
Then I used called the script on the client from mosh like this:
function tmosh() {
mosh $1 -- tmux-reattach-if-exists
}
If there's a solution that can do this via .tmux.conf directly that would be great but I couldn't seem to work that out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With