j.con
j.con

Reputation: 889

Nested ssh run commands on 2nd server

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?

Upvotes: 6

Views: 6915

Answers (2)

chepner
chepner

Reputation: 530922

Use the -J option.

ssh $hostB -J $hostA "..."

Upvotes: 3

nu11p01n73R
nu11p01n73R

Reputation: 26667

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

Upvotes: 3

Related Questions