如何通过shell脚本中的ssh使用另一台服务器连接到服务器? [英] How to connect to a server using another server by ssh in shell script?
问题描述
场景是这样的:
SERVER_A="servera.com"
SERVER_A_UNAME="usera"
SERVER_B="serverb.com"
SERVER_B_UNAME="userb"
我想写一个shell脚本,先连接到服务器A,然后才连接到服务器B.比如:
I want to write a shell script which will fist connect to server A, and then only it would be connected to server B. Like:
#!/bin/sh
ssh $SERVER_A_UNAME@$SERVER_A ...and then
ssh $SERVER_B_UNAME@$SERVER_B
但是我做不到.它只连接到服务器 A.我怎样才能实现它?
But I am not able to do it. It does connect to server A only. How can I achieve it?
推荐答案
你应该看看 ssh ProxyCommands
,它可以让你自动进行间接连接.基本上你把以下内容放在你的 .ssh/config
you should have a look at ssh ProxyCommands
that lets you do indirect connects automatically. basically you put the following in you .ssh/config
Host gateway1
# nichts
Host gateway2
ProxyCommand ssh -q gateway1 nc -q0 gateway2 22
Host targethost
ProxyCommand ssh -q gateway2 nc -q0 targethost 22
然后即使无法直接访问目标主机,您也可以成功运行ssh targethost
.您可以阅读更多关于此的信息,例如这里http://sshmenu.sourceforge.net/articles/transparent-mulithop.html
and then you can run ssh targethost
successfully even if targethost is not reachable directly. you can read more about this e.g. here http://sshmenu.sourceforge.net/articles/transparent-mulithop.html
这篇关于如何通过shell脚本中的ssh使用另一台服务器连接到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!