Go root, create tmux, send commands and then attach - all via a single SSH command in a bash script

890 views Asked by At

How would you go about sending a command via SSH:

ssh user@server -t

Which will create a tmux session, send commands to it and then attach - allowing you to manually work on the interactivity presented from the commands? This has to be done while at the same time logging in as root - NOT via sudo.

ssh user@server -t "bash -c \"su - -c \"tmux -d \; apt-get update \; apt-get upgrade\" root\""

or simply without bash -c (which was an attempt at getting SSH to show the tmux window - as it (when using su - instead of sudo) would not display the shell. This was only when running it from a script - when run directly as a shell command there was no issue.

Another attempt have been:

ssh user@server -t "su - -c \"tmux new-session -n $session_name && tmux send-keys -t $session_name \"$command\"\" root"

This latter work relatively well - only it attaches to the tmux session and as such commands are sent after exiting the tmux manually only.

The idea is basically to automatically create a tmux session in which some commands are run - and then attach to it. This is to be done as a part of a bash script.

Any ideas?

1

There are 1 answers

0
cxou On

For those interested, the command I found to work was:

ssh -p22 user@server -t "su - root -c \"tmux -d $session_name\;\"-t $session_name -c \"apt-get update\"\" \; \"-t $session_name -c \"apt-get upgrade\"\"\""

However it does not make you enter the tmux - merely sends back the input from the tmux to the user. A step in the right direction but not quite there..!

Very open to more efficient answers!