ansible一键连接脚本
#!/bin/bash
read -ep '请输入起始IP地址:' ip_start
read -ep '请输入结束IP地址:' ip_stop
read -ep '请输入默认统一密码' Ps
Date=$(date +%Y%m%d)
L4=$(echo $ip_start | awk -F "." '{print $4}' )
L3=$(echo $ip_stop | awk -F . '{print $4}')
L2=$(echo $ip_start | awk -F . '{print $1"."$2"."$3"."}')
expect << EOF
spawn ssh-keygen
expect {
"*rsa*" { send "\r";exp_continue}
"*Overwrite*" { send "yes\r";exp_continue}
"*passphrase*" { send "\r";exp_continue}
"*again*" { send "\r";exp_continue}
}
EOF
echo $L2$L4
echo $L2$L3
if [ "$L4" -le "$L3" ] ; then
for ((i=$L4;i<=$L3;i++ ))
do
ping -c 2 $L2$i 2&> /dev/null
if [ $? -ge 0 ];then
expect << EOF
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub root@$L2$i
expect {
"*fingerprint*" {send "yes\r";exp_continue}
"*password*" { send "$Ps\r";exp_continue}
}
EOF
echo "$Date" >> succ.txt
echo "--------------------" >> succ.txt
echo "$L2$i" >> succ.txt
echo "$L2$i---------->OK "
fi
done
fi
posted on 2020-11-12 10:10 ChaoYue_Xu 阅读(81) 评论(0) 编辑 收藏 举报