http://www.cnblogs.com/panchong/p/6027138.html?utm_source=itdadao&utm_medium=referral
# 连接新主机时,不进行公钥确认 ssh-copy-id -o StrictHostKeyChecking=no root@10.10.5.133 # 安装sshpass包 yum install sshpass # 将ssh公钥复制到远程主机 sshpass -p 'YOUR_PASSWORD' ssh-copy-id -o StrictHostKeyChecking=no root@10.10.5.133 # 可以将远程主机的域名或IP地址记录在一个文件中,比如记录在remote-hosts文件中,运行下面的脚本就能批量的将公钥复制到远程主机中 for host in $(cat remote-hosts) do sshpass -p 'YOUR_PASSWORD' ssh-copy-id -o StrictHostKeyChecking=no root@${host} done # 如果每台主机的密码不一样,可以将密码记录在remote-hosts文件中,通过cut命令分割,可以分别获得主机的IP地址或域名和对应的密码,当然如果ssh的端口号不是默认的22,也可以一并记录。如下列格式: 10.10.10.10:2222:YOURPASSWORD #脚本如下 for host in $(cat remote-hosts) do ip=$(echo ${host} | cut -f1 -d ":") port=$(echo ${host} | cut -f2 -d ":") password=$(echo ${host} | cut -f3 -d ":") sshpass -p ${password} ssh-copy-id -p ${port} -o StrictHostKeyChecking=no root@${ip} done