user3595632
user3595632

Reputation: 5730

How to execute consecutive commands using ssh -t

What I'm trying to do:

ssh somewhere@ip -t "tmux && echo 'a'"

and intention is: 'access to a remote server(ip) and open tmux and echo "a" on that tmux session'.

But what it missed is echo 'a': it prints 'a' in my local machine, not remote server's tmux session (even after I exit the tmux session manually)

How can I do it?

Upvotes: 0

Views: 256

Answers (1)

Mark
Mark

Reputation: 4455

Consider using an expect script to solve your problem:

#!/usr/bin/expect


spawn ssh somewhere@ip 

interact {

-o -nobuffer "$"  {
        send "tmux\n"
}

}

The above script won't get the job done. Basically, it will look for a $ in the ssh session output (let's say your prompt ends with a '$') and it will send the characters "tmux" and a linefeed. That will start your tmux session. However, you'll have to add an additional case to send the "a\n" to tmux when the tmux session prompts the user interactively.

You may have to play a bit to get expect to work for you. I'm no expert on expect, but it may be the right tool for the job.

Upvotes: 1

Related Questions