ssh判断免密登陆

ssh判断免密登陆


[root@jenkins ~]# vi /opt/release_code.sh
#!/bin/bash

. /etc/init.d/functions

#echo $WORKSPACE
#echo $JOB_NAME

Remote_IP="10.0.0.7"

ssh $Remote_IP -o PreferredAuthentications=publickey -o StrictHostKeyChecking=no "ls" &> /dev/null

if [ $? -eq 0 ]; then
    action  "目标主机免密连接正常" /bin/true
else
    echo
    echo -e "*******免密失败,分发密钥*******\n"
    sshpass -p1 ssh-copy-id -i ~/.ssh/id_rsa.pub root@$Remote_IP -o StrictHostKeyChecking=no &>/dev/null
    if [ $? -eq 0 ];then
        action "$Remote_IP : 密钥分发成功" /bin/true
        echo
    else
        action "$Remote_IP : 密钥分发失败" /bin/false
        echo
    fi
fi

posted @ 2020-09-25 11:29  Gshelldon  阅读(598)  评论(0编辑  收藏  举报