Linux批量推送公钥(免密登录)

centos7环境的脚本

本机必需要有expect或者配置了yum

添加被推送的ip
vi iplist.txt
password=修改为被推送机器密码
ip.log是可以ping通的机器


代码如下:

#!/usr/bin/bash
>ip.log
password=CentOS
if [ ! -f ~/.ssh/id_rsa ];then
	ssh-keygen  -P "" -f ~/.ssh/id_rsa
fi

rpm -q expect &>/dev/null
if [ $? -ne 0 ];then
	yum -y install expect
fi

while read ip 
do
{

ping -c1 -W1 $ip &>/dev/null
	if [ $? -eq 0 ];then
		echo $ip >>ip.log
		/usr/bin/expect <<-EOF
		spawn ssh-copy-id $ip
		expect {
			"yes/no" { send "yes\r" ; exp_continue }
			"password:" { send "$password\r" };
		}
		expect eof
		EOF
	fi
}&
done<iplist.txt
wait
echo "all finish"

你可能感兴趣的:(linux,shell)