expect模块的使用
第一步首先下载expect 模块,yum list |grep expect ,安装下面的模块.
expect.x86_64 5.44.1.15-5.el6_4 @base
expect-devel.x86_64 5.44.1.15-5.el6_4 @base
安装完成后,可以在程序which expect下面可以查看环境/usr/bin/expect,需要expect和tcl两个包
rpm -qa tcl
tcl-8.5.7-6.el6.x86_64
[root@ob ~]# rpm -qa | grep expect
expect-5.44.1.15-5.el6_4.x86_64
expect-devel-5.44.1.15-5.el6_4.x86_64
贴两个代码
cat test1.sh #!/usr/bin/expect SERVERS="192.168.79.129 192.168.79.131" PASSWORD=123456 auto_ssh_copy_id() { echo "2" expect -c "set timeout -1; spawn scp -r /root/app root@$1:/root/; expect { *(yes/no)* {send -- yes\r;exp_continue;} *assword:* {send -- $2\r;exp_continue;} eof {exit 0;} }"; } sh_copy_id_to_all() { echo "111" for SERVER in $SERVERS do auto_ssh_copy_id $SERVER $PASSWORD echo ${SERVER} done } sh_copy_id_to_all
next code
SERVER="192.168.79.129 192.168.79.131" auto_zookp_cp() { expect {*yes/no* {send "yes\r"} expect {*password* {send "123456\r"} } for myserver in ${SERVER} do scp -r /root/app root@${myserver}:/root/ auto_zookp_cp done
use sh to execute the shell,otherwise it wiil produce an error.
时来天地皆同力,运去英雄不自由