[root@localhost ~]# cat auto_expect.sh
#!/bin/bash
#Date: 2013-09-10
#Author: bourneye
#Pupose: autorun remote script
#remote host ips
ips=(192.168.10.134 192.168.10.135)
#local host pub_key
pub_key=`cat /root/.ssh/id_rsa.pub`
for ip in ${ips[*]}
do
com="ssh root@$ip"
expect -c "
spawn $com;
expect {
\"yes/no\" {send \"yes\r\";exp_continue}
\"password:\" {send \"123456\r\";exp_continue}
}
expect "]*"
send \"echo $pub_key > /root/.ssh/authorized_keys\r\"
send \"wget ftp://192.168.10.128/*\r\"
send \"chmod 755 *.sh\r\"
send \"echo 'nohup sh init.sh;sh install_mysqld.sh &'|at now +1 min\r\"
expect eof
"
done
#簡單理解:expect=if,send=do,但不可等同。
##注意,expect eof會退出連接狀態,中斷執行中的腳本;設置1分鐘後(視具體情況加減)執行。