007 expect
一、安装
yum -y install expect
二、脚本中使用 (实例)
auto_keygen (){
/usr/bin/expect<<EOF
#超时时间
set timeout 30
#执行需要进行捕获的shell语句
spawn ssh-keygen
#开始捕获
expect {
#捕获之后,使用 ‘send’ 发送命令 exp_continue表示继续捕获
".ssh/id_rsa)" { send "\n"; exp_continue }
"Overwrite (y/n)?" { send "y\n"; exp_continue }
"no passphrase):" { send "\n"; exp_continue }
"again:" { send "\n"; exp_continue }
}
EOF
}
send_key () {
pwd=upsa
/usr/bin/expect <<EOF
set timeout 30
# 发送公钥给对方服务器
spawn ssh-copy-id root@$1
expect {
"yes/no" { send "yes\n"; exp_continue }
"password:" { send "${pwd}\n"; exp_continue }
}
expect eof
EOF
}
pub_key_file=$HOME/.ssh/id_rsa.pub
if [ ! -f ${pub_key_file} ];then
auto_keygen
fi
for ip in 192.168.111.136
do
send_key $ip
done