[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分钟后(视具体情况加减)执行。
转载于:https://blog.51cto.com/bourneye/1293807