第一次通过脚本运行 ssh-copy-id 时回答“是”?

San*_*dra 10 linux ubuntu bash centos

第一次ssh-copy-id会问

# ssh-copy-id -i .ssh/id_dsa.pub backup@example.com
The authenticity of host 'example.com (xxx.xxx.xxx.xxx)' can't be established.
RSA key fingerprint is 39:fb:5e:70:30:33:2b:18:17:e9:4f:2f:91:b5:d2:21.
Are you sure you want to continue connecting (yes/no)? 
Run Code Online (Sandbox Code Playgroud)

是否可以编写此脚本,因此脚本只会回答是?

bra*_*n99 10

SSH 有一个选项可以自动将任何主机密钥添加到受信任的主机文件中:

ssh-copy-id -i .ssh/id_dsa.pub -o StrictHostKeyChecking=no backup@example.com
Run Code Online (Sandbox Code Playgroud)

作为替代方案,您可以执行以下操作:

echo "yes \n" | ssh-copy-id -i .ssh/id_dsa.pub backup@example.com
Run Code Online (Sandbox Code Playgroud)

编辑:由于这些解决方案似乎不适用于ssh-copy-id,因此您始终可以创建一个~/.ssh/config包含以下选项的文件:

StrictHostKeyChecking no
Run Code Online (Sandbox Code Playgroud)

这应该适用于所有 SSH 连接,无论它们是否通过脚本调用。


cjc*_*cjc 7

如果您ssh-copy-id不支持 StrictHostKeyChecking 选项,您可以编写一个执行以下操作的脚本:

  1. ssh-keyscan针对目标服务器运行以获取公钥
  2. 将其附加到 known_hosts 文件
  3. ssh-copy-id