参考:https://www.cnblogs.com/gshelldon/p/13729036.html
[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