前言

今天在工作中需要批量给集群服务器安装jdk,属于解包安装那种,运行到一台服务器给内网其他服务器传送文件的时候报如下错误

UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: Shared connection to 192.168.xx.xx closed.", "unreachable": true}

根据这条提示我们可以看出是ssh在很久没人操作时,自动关闭了链接,于是到目标主机运行该脚本:

#!/bin/bash
#

for i in {xx..xx};do
    scp -o "StrictHostKeyChecking no" -i /tmp/key  /tmp/jdk-8u181-linux-x64.tar.gz root@192.168.xx.${i}:/tmp/
    scp -o "StrictHostKeyChecking no" -i /tmp/key  /tmp/install.sh root@192.168.xx.${i}:/tmp/
done

运行结果如下:

卡在密钥确认中

解决

为解决在在脚本中出现的这种情况

方法一:添加选项

比如:

#-o "StrictHostKeyChecking no"
scp -o "StrictHostKeyChecking no" -i /tmp/key  /tmp/install.sh  # 添加选项-o "StrictHostKeyChecking no" 表示不验证密钥

方法二:修改配置文件

# vim /etc/ssh/ssh_config
StrictHostKeyChecking ask   
# 将上面这行修改为
StrictHostKeyChecking no

最后重载配置文件

systemctl reload sshd
Last modification:June 2nd, 2020 at 11:32 am
If you think my article is useful to you, please feel free to appreciate