EXPECT交互式批量上传公钥
EXPECT交互式批量上传公钥
# vim key.sh
#/bin/bash Public_Key() { [ ! -f /usr/bin/expect ] && yum install expect -y &>/dev/null #判断expect是否安装,没有则安装expect(这个挺好用的) /usr/bin/expect <<EOF #在控制节点机器上生成密钥对,一种是从未生成过,一种是原来生成过(注意本脚本选择不覆盖) set timeout 30 spawn ssh-keygen expect "(/root/.ssh/id_rsa):" send "\n" expect { "Overwrite" { send "n\n" } "Enter passphrase (empty for no passphrase):" { send "\n" expect "Enter same passphrase again:" send "\n" } } expect eof EOF while read line;do #遍历循环/test/host_passwd.txt文件,批量上传公钥 host=$(echo $line | awk '{print $1}') passwd=$(echo $line | awk '{print $2}') /usr/bin/expect <<EOF spawn ssh-copy-id $host expect { "yes/no" { send "yes\n"; exp_continue } "password:" { send "$passwd\n"} } expect eof EOF done < /test/host_passwd.txt #主机ip及密码文件的绝对路径
} Public_Key && echo "now you can ssh ip" || echo "Please check your host_passwd.txt"
# cat /test/host_passwd.txt
192.168.10.10 123456 192.168.10.11 123456 192.168.10.12 123456 192.168.10.13 123456 ...
完成后删除 rm -rf /test/host_passwd.txt 即可