Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested ssh run commands on 2nd server

Tags:

bash

ssh

I can only get to hostB from hostA, and I want to run commands on hostB.

ssh -t $hostA ssh -t $hostB "

   echo 'Hello World!'

   echo 'Test!'

"

At the moment, this will connect to hostA then hostB and the script will pause. As soon as i type exit (from hostB) I return to hostA, the 2 echo commands are printed and then automatically exits from hostA.

How can I run commands on hostB?

like image 586
j.con Avatar asked Oct 20 '22 22:10

j.con


1 Answers

Changing code as using here document and sshpass can do the trick

ssh -T user@$hostA <<EOA
sshpass -p password ssh  -T user@$hostB <<EOB
echo hello 
EOB
EOA
like image 71
nu11p01n73R Avatar answered Oct 22 '22 12:10

nu11p01n73R