Home > Software design >  How can I run commands in nested SSH connections in Bash?
How can I run commands in nested SSH connections in Bash?

Time:12-13

I need to write a script which will connect to the server and run some utils there.

So, if I want to connect to the server, I do

ssh $server << EOF
run
some
commands
EOF

And it works properly. But if I want to do nested ssh connection, I'm doing like this:

ssh $server_1 << EOF
ssh $server_2 << EOF
run some commands
EOF

I guess it works properly, but I'm receiving error messages Do you know how to use "nested" EOFs properly? I know that I can run

ssh $server 'run|some|commands' 

but there are a lot of commands here and I cant write it into a line

Thank you for answers

CodePudding user response:

Use two different "end of here-document" delimiters:

ssh $server_1 << EOF1
ssh $server_2 << EOF2
run some commands
EOF2
EOF1

Or better yet, use an "SSH jump host" like this:

ssh -J $server_1 $server_2 << EOF
run some commands
EOF
  • Related