简体   繁体   中英

How can I run commands in nested SSH connections in Bash?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM