链 SSH 脚本

Chain SSH script

假设我有 3 linux 个服务器。

我需要一个脚本,它将通过 ssh 从 s1 登录到 s2,然后通过 ssh 从 s2 登录到 s3,然后检查 ~/.ssh/authorized_keys 中是否已经存在来自 s1 的字符串然后什么也不做否则做

echo '${id_rsa.pub from s1}' >> ~/.ssh/authorized_keys_on_s3

s1和s2是常量,s3可以是变量。 从 s1 到 s2 的 ssh 已经设置好无密码, 从 s2 ssh 到所有 s3 已经设置好无密码。

我需要添加 s3 的 ip 作为脚本的输入。

我该怎么做?

您可以将 ProxyJumpssh-copy-id

结合使用

通过 s2 从 s1 连接 s3

user@s1:~$ ssh -o JumpProxy=s2 s3

当 public 密钥不存在时将其复制到 s3

user@s1:~$ ssh-copy-id -o JumpProxy=s2 s3