如何通过shell脚本中的ssh使用其他服务器连接到服务器?

时间:2013-09-03 16:03:59

标签: shell ssh

场景如下:

SERVER_A="servera.com"
SERVER_A_UNAME="usera"
SERVER_B="serverb.com"
SERVER_B_UNAME="userb"

我想写一个shell脚本,它将连接到服务器A,然后只连接到服务器B.喜​​欢:

#!/bin/sh
ssh $SERVER_A_UNAME@$SERVER_A ...and then
ssh $SERVER_B_UNAME@$SERVER_B

但我无法做到。它仅连接到服务器A.我怎样才能实现它?

2 个答案:

答案 0 :(得分:0)

您可以在上一个问题上找到一些帮助:

How to use bash/expect to check if an SSH login works

根据您的具体情况,您还可以执行远程ssh命令并等待正面反馈。

请参阅:

How to use SSH to run a shell script on a remote machine?

答案 1 :(得分:0)

你应该看一下ssh ProxyCommands,它可以让你自动进行间接连接。基本上你把以下内容放在.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

然后即使无法直接访问targethost,您也可以成功运行ssh targethost。你可以阅读更多关于这个这里http://sshmenu.sourceforge.net/articles/transparent-mulithop.html

相关问题