-2

When I try more than one command to remotely execute commands through ssh inside other ssh, I get weird result.

From man ssh:

-t

Force pseudo-terminal 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 I do

ssh -t root@host1 ssh root@host2 "cat /etc/hostname && cat /etc/hostname"

or

ssh -t root@host1 ssh -t root@host2 "cat /etc/hostname && cat /etc/hostname"

in both cases I get

host1
Connection to host1 closed.
host2
Connection to host2 closed.

I want this result:

host1
host1
Connection to host1 closed.
Connection to host2 closed.

I want to run all commands in same server using ssh inside ssh.

If I use only one ssh, it works:

ssh -t root@host1 "cat /etc/hostname && cat /etc/hostname"
host1
host1
Connection to host1 closed.
jww
  • 97,681
  • 90
  • 411
  • 885

2 Answers2

0

I get it work, but I can not explain what is happening.

ssh -t root@host1 "ssh -t root@host2 \"cat /etc/hostname ; cat /etc/hostname\""
host1
host1
Connection to host1 closed.
Connection to host2 closed.
  • Can someone explain why only one pair of quotes not work? – Francisco Panis Kaseker Oct 02 '19 at 15:58
  • 1
    With only one pair of quotes, the string `ssh -t root@host2 cat /etc/hostname && cat /etc/hostname` is passed to host 1. (ie, the quotes have been removed by the shell). You need the embedded quotes to send the compound command to host2. – William Pursell Oct 02 '19 at 23:49
0

Try:

That's not how -t works.

For your option try:

 ssh root@host1 .....; ssh root@host2 ....

Otherwise, use PSSH that will do the uptime command on both servers at same time:

pssh -h hostfile -i uptime
wuseman
  • 1,259
  • 12
  • 20