通过脚本从另一个 ssh 连接到 ssh

通过脚本从另一个 ssh 连接到 ssh

我有一个通过 ssh 连接到 AWS 的脚本。脚本的内容非常简单:

#! /bin/bash
ssh -i private_key.pem [email protected]

我从终端运行这个脚本:

./connect

所以我连接到了这个 AWS。

我想要做的是将一个 AWS 连接到第二个 AWS。基本上,运行脚本后我可以输入

ssh second_aws

但我想在一个脚本中执行此操作,以便运行该脚本并且我的终端将连接到第二个 AWS,例如:

 #! /bin/bash
 ssh -i private_key.pem [email protected]
 ssh second_aws

但如果我运行它,它会移动到一个 AWS,并且不会连接到第二个 AWS。

如何使用该脚本,从终端运行它,并且我的终端将连接到第二台计算机?

答案1

考虑只告诉第一个主机执行ssh命令,而不是给你一个 shell:

ssh -t -i private_key.pem [email protected] ssh -t second_aws

答案2

您所做的操作不会打开 AWS 实例的第二个连接。它从启动脚本的位置打开连接。

您应该寻找该ProxyJump选项。它将允许您链接多个 SSH 连接。

这是该页面的摘录man

 ProxyJump
         Specifies one or more jump proxies as either [user@]host[:port] or an ssh URI.  Multiple proxies may be separated by comma characters and will be visited sequentially.  Setting this option will cause ssh(1) to connect
         to the target host by first making a ssh(1) connection to the specified ProxyJump host and then establishing a TCP forwarding to the ultimate target from there.

         Note that this option will compete with the ProxyCommand option - whichever is specified first will prevent later instances of the other from taking effect.

         Note also that the configuration for the destination host (either supplied via the command-line or the configuration file) is not generally applied to jump hosts.  ~/.ssh/config should be used if specific configuration
         is required for jump hosts.

相关内容