#实现创建秘钥,批量分发公钥到服务端

一:ansible必备准备

1. 实现创建秘钥,批量分发公钥到服务端

#/bin/bash
#by Egrep
#Create a secret key pair
/bin/rm -rf  /root/.ssh/* && ssh-keygen -t dsa -P "" -f ~/.ssh/id_dsa -q

#Public Key Distribution
for ip in $*
do
sshpass -pkai  ssh-copy-id -i /root/.ssh/id_dsa.pub  -o StrictHostKeyChecking=no $ip
#
sshpass -pkai ssh-copy-id -i /root/.ssh/id_dsa.pub -o StrictHostKeyChecking=no $ip #这个命令有个小坑 在centos6上需要 -o 参数后 "-o StrictHostKeyChecking=no" 在centos7 上不需要
done
#!/bin/bash
#push publick
#by Egrep
password=uplooking
>ip.txt
#判断expect包是否安装
rpm -q expect &>/dev/null
    if [ $? -ne 0 ];then
        yum -y install expect
fi

#判断公钥是否存在
if [ ! -f ~/.ssh/id_rsa ];then
    ssh-keygen -P "" -f ~/.ssh/id_rsa
fi

for i in {2..254}
do 
    {
    ip=192.168.122.$i
    ping -c1 $ip &>/dev/null
    if [ $? = 0 ];then
        echo "$ip" >> ip.tet
        /usr/bin/expect <<-EOF
        set timeout 10
        spawn ssh-copy-id $ip
        expect {
            "yes/no" { send "yes\r"; exp_continue }
            "password:" { send "$password\r" }

        }
        expect eof
        EOF
    fi
    }&
done
wait
echo successfull.......

 

2 sshpass非交互方式工具使用

命令作用:用于非交互ssh密码验证

常用参数:
-f  给出密码文件路径
-p  指定明文密码

ssh登陆不能在命令行中指定密码,也不能以shell中随处可见的,sshpass 的出现,解决了这一问题。它允许你用 -p 参数指定明文密码,然后直接登录远程服务器。 它支持密码从命令行,文件,环境变量中读取

 

posted @ 2018-04-18 12:56  运维面试辅导  阅读(241)  评论(0)    收藏  举报