I'm looking for a way to automate the start-up of my development environment. I have three virtual machines that have to be started, then have to ssh
to each of them and open VPN on them.
So far I've gotten them to start and managed to ssh to 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 [email protected] &
gnome-terminal --title "virtual_2: 11.100" -x ssh [email protected] &
gnome-terminal --title "virtual_1: 12.100" -x ssh [email protected] &
How do I execute an additional command in each of the terminals which starts openvpn?
For simplicity I'm trying to echo 1
in each terminal instead of starting VPN.
I've found that multiple commands on terminal start can be run like:
gnome-terminal -x bash -c "cmd1; cmd2"
So for one terminal to keep it simple I changed:
gnome-terminal --title "virtual_3: server" -x ssh [email protected] &
to:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh [email protected] ; echo 1" &
But 1
wasn't printed in the terminal of virtual_3.
Then I thought, maybe the command is being executed too quickly, before the terminal is ready, so I tried adding &&
:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh [email protected] &&; echo 1" &
But that gave no result either.
First of all, if you run
gnome-terminal -x bash -c "cmd1; cmd2"
you get bash
to execute cmd1
and cmd2
. It doesn't first execute cmd1
and then give cmd2
to its result. ssh
is a program run in the terminal and your cmd2
won't be executed until that is finished.
So you need to run ssh
and tell that to execute your command.
You can do so by:
ssh user@address "command_to_execute"
However, ssh
exits after the command is finished. As you can see in "With ssh, how can you run a command on the remote machine without exiting?", you can execute ssh
with the -t
option so it doesn't quit:
ssh -t user@address "command_to_execute"
So your command in the end becomes:
gnome-terminal --title "virtual_3: server" -x bash -c "ssh -t [email protected] 'echo 1'"
You are right, giving -t
alone is not enough (although necessary). -t
allocates buffer for the tty
but doesn't execute bash for you. From the manual of ssh
:
-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
If
command
is specified, it is executed on the remote host instead of a login shell.
So what you need is, to execute bash your self. Therefore:
ssh -t user@address "command_to_execute; bash"
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