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

贴两个代码

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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 -/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

1

2

3

4

5

6

7

8

9

10

11

12

13

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 -/root/app root@${myserver}:/root/

    auto_zookp_cp

done

  use sh to execute the shell,otherwise it wiil produce an error.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值