简体   繁体   English

如何在 Bash 中的嵌套 SSH 连接中运行命令?

[英]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 连接,我会这样做:

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?我猜它工作正常,但我收到错误消息你知道如何正确使用“嵌套”EOF吗? 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 跳转主机”:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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