There is often a problem: to connect to a specific server via SSH, after connecting, always perform the same sequence of three commands there, and then there may be some different actions.

I want to automate the first part of the process.

I managed to write a bash script that, when launched, opens a separate terminal window, performs an SSH connection and leaves the terminal window open:

#! /bin/bash xfce4-terminal -e 'ssh username@host' 

Here's how I still have to execute three more predetermined commands in this ssh connection session? I have little understanding of the syntax of bash scripts. I tried to just add these commands like this:

 #! /bin/bash xfce4-terminal -e ' ssh username@host комманда_1 комманда_2 комманда_3 ' 

And at least the line with ssh is executed. I understand this by the fact that in the terminal window a passphrase for the ssh key is requested, and if entered incorrectly, it asks two more times - exactly as in a normal ssh connection.

But when you enter the correct passphrase, the terminal window simply closes immediately.

  • command_1 && command_2 && command_3 - Senior Pomidor
  • @SeniorAutomator but where should I write it? On the same line with ssh-connection? Inside the same single quotes? I tried a couple of options, I have something wrong. I need that after executing these three commands, the ssh connection remains open and I can continue to work with it manually. - Xander
  • here is an example ssh username@host 'ls -l; ps -aux; whoami' ssh username@host 'ls -l; ps -aux; whoami' ssh username@host 'ls -l; ps -aux; whoami' put a sign; between teams and take in quotes - Senior Pomidor
  • so that after executing these three commands, the ssh connection remains open and I can continue to work with it manually - this is a completely different question. - aleksandr barakin

1 answer 1

the list of commands passed to the ssh program must be enclosed in quotes:

 #!/bin/bash xfce4-terminal -e 'ssh username@host "команда1; команда2; команда3"' 

so that after all the commands are executed, the ssh program does not end:

  • before pressing enter , the last command can be specified, for example, read
  • some time, the last command you can specify sleep 10 (pause at ten seconds)

There must be no gaps in the shangang !