How to create a new terminal session and execute several commands

I am looking for a way to automate the launch of my development environment. I have three virtual machines that need to be started, then I need ssh for each of them and open a VPN on them.

So far, I got them to run and got ssh for them:

 #!/bin/sh virsh start virtual_1 virsh start virtual_2 virsh start virtual_3 sleep 2m gnome-terminal --title "virtual_3: server" -x ssh root@192.168.1.132 & gnome-terminal --title "virtual_2: 11.100" -x ssh root@192.168.11.100 & gnome-terminal --title "virtual_1: 12.100" -x ssh root@192.168.12.100 & 

How to execute an additional command on each of the terminals that run openvpn?

For simplicity, I try echo 1 in each terminal instead of starting a VPN.

I found that you can run several commands when starting the terminal, for example:

 gnome-terminal -x bash -c "cmd1; cmd2" 

So, for one terminal, to be simple, I changed:

 gnome-terminal --title "virtual_3: server" -x ssh root@192.168.1.132 & 

in

 gnome-terminal --title "virtual_3: server" -x bash -c "ssh root@192.168.1.132 ; echo 1" & 

But 1 not printed in terminal virtual_3. Then I thought, maybe the command runs too fast until the terminal is ready, so I tried adding && :

 gnome-terminal --title "virtual_3: server" -x bash -c "ssh root@192.168.1.132 &&; echo 1" & 

But this also did not produce a result.

+8
bash
source share
1 answer

First of all, if you run

 gnome-terminal -x bash -c "cmd1; cmd2" 

you will get bash to execute cmd1 and cmd2 . It does not execute cmd1 first, and then cmd2 returns its result. ssh is a program running in the terminal, and your cmd2 will not execute until it is complete.

So, you need to run ssh and say what to execute your command.

You can do it:

 ssh user@address "command_to_execute" 

However, ssh exits after the command completes. As you can see in Using ssh, how can you run a command on a remote computer without exiting? ", you can execute ssh with the -t option so it doesn’t go away:

 ssh -t user@address "command_to_execute" 

So your team at the end will be:

 gnome-terminal --title "virtual_3: server" -x bash -c "ssh -t root@192.168.1.132 'echo 1'" 

You are right, one -t not enough (although necessary). -t allocates a buffer for tty but does not execute bash for you. From the ssh manual:

-t Assign pseudo-tty distribution. This can be used to execute arbitrary programs on the screen on a remote machine , which can be very useful, for example. when implementing menu services. Many -t options force tty, even if ssh does not have a local tty.

If command specified, it runs on the remote host instead of the login shell.

So, you need to run bash your self. Therefore:

 ssh -t user@address "command_to_execute; bash" 
+11
source share

All Articles