20.31 expect脚本同步文件

 

将某台机器上的文件拷贝到本机指定目录:

[root@hyc-01 ~]# vim 4.expect

#!/usr/bin/expect

set passwd "hyc940421"

spawn rsync -av root@192.168.31.129:/tmp/12.txt /tmp/

使用rsync工具以root登录192.168.31.129,拷贝/tmp/下的12.txt到本机的/tmp/目录

expect {

"yes/no" {send "yes\r"}

"password:" {send "$passwd\r"}

}

expect eof

若无expect eofrsync还未来得及传输文件就被退出终端

 

[root@hyc-01 ~]# chmod a+x 4.expect

[root@hyc-01 ~]# ./4.expect

spawn rsync -av root@192.168.31.129:/tmp/12.txt /tmp/

root@192.168.31.129's password:

receiving incremental file list

12.txt

 

sent 49 bytes  received 104 bytes  102.00 bytes/sec

total size is 13  speedup is 0.08

[root@hyc-01 ~]# cat /tmp/12.txt

123456 22222

 

20.32 expect脚本指定host和要同步的文件

 

[root@hyc-01 ~]# vim 4.expect

#!/usr/bin/expect

set user [lindex $argv 0]

set host [lindex $argv 1]

set passwd "hyc940421"

set cm [lindex $argv 2]

spawn ssh $user@$host

 

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect "]*"

send "$cm\r"

set timeout 5

expect "]*"

send "exit\r"

 

[root@hyc-01 ~]# ./4.expect root 192.168.31.129 "vmstat 1 10"

spawn ssh root@192.168.31.129

root@192.168.31.129's password:

Last login: Thu Oct  4 11:11:58 2018 from 192.168.31.128

[root@hyc-01-01 ~]# vmstat 1 10

procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----

 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st

 3  0      0 561720   2076 640364    0    0     2    53  120  150  0  0 99  0  0

 0  0      0 561720   2076 640404    0    0     0    11  162  192  1  0 98  1  0

 1  0      0 561720   2076 640404    0    0     0    15  144  180  0  1 99  0  0

 1  0      0 561720   2076 640404    0    0     0     6  115  151  0  0 100  0  0

 0  0      0 561720   2076 640404    0    0     0   450  140  167  0  2 97  1  0

1不设置

set timeout -1

expect "]*"

send "exit\r"

则会马上退出终端

2 设置

expect "]*"

send "exit\r"

则执行vmstat 1 20时会停留10s然后退出终端

3 设置

set timeout -1

expect "]*"

send "exit\r"

则会一直执行vmstat 1 20直到执行结束并且会一直留在当前终端

4 设置

set timeout -1

expect eof

则会一直执行vmstat 1 20直到执行结束并会一直留在当前终端

5 只设置

set timeout -1

则仍会立刻退出当前终端

 

set timeout后跟的数字为正数则表示会在终端停留指定时长;

为负数表示(-1)会永久停留在当前终端;

 

指定host和要同步的文件:

[root@hyc-01 ~]# vim 1.expect

#!/usr/bin/expect

set passwd "hyc940421"

set host [lindex $argv 0]

set file [lindex $argv 1]

spawn rsync -av $file root@$host:$file       

expect {

"yes/no" {send "yes\r"}

"password:" {send "$passwd\r"}

}

expect eof

 

[root@hyc-01 ~]# ./1.expect 192.168.31.129 /tmp/21.txt

spawn rsync -av /tmp/21.txt root@192.168.31.129:/tmp/21.txt

root@192.168.31.129's password:

sending incremental file list

21.txt

 

sent 104 bytes  received 35 bytes  278.00 bytes/sec

total size is 13  speedup is 0.09

[root@hyc-01-01 tmp]# ls 21.txt

21.txt

 

20.33 构建文件分发系统

 

需求:

需要自动同步多个文件到多台机器

思路:

有一台模板机器,准备好要分发的文件,使用expect脚本批量将需要同步的文件分发到目标机器

核心命令:

rsync -av --files-from=list.txt $file $user@$host:$file

 

[root@hyc-01 sbin]# vim rsync.expect

 

#!/usr/bin/expect

set passwd "hyc940421"

设置密码为hyc940421

set host [lindex $argv 0]

设置将第一个参数赋给变量host

set file [lindex $argv 1]

将第二个参数赋给变量file,该参数为一个文件的内容,内容为要同步的文件的绝对路径;

spawn rsync -avR --files-from=$file / root@$host:/

这里同步的源目录和目标目录为根;

若无法保证所有目标机器都存在指定的绝对路径,则需要加R参数,此时若被同步机器不存在文件的绝对路径则会自动创建该路径(创建级联目录);

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect eof

 

所有被同步文件的绝对路径:

[root@hyc-01 sbin]# vim file.list

/root/root.txt

/tmp/21.txt

 

所有被同步机器的ip

[root@hyc-01 sbin]# vim ip.list

192.168.31.129

127.0.0.1

 

此时要保证所有被同步机器使用相同的密码;

如果密码不同,则需要编辑专门的密码文件,若该文件泄露则对机器很不安全;

可以使用密钥认证,此时不需要输入密码;.

 

创建rsync.sh用于遍历ip地址:

[root@hyc-01 sbin]# vim rsync.sh

#!/bin/bash

for ip in `cat /usr/local/sbin/ip.list`

do

  ./rsync.expect $ip /usr/local/sbin/file.list

done

 

执行:

[root@hyc-01 sbin]# chmod a+x rsync.expect

[root@hyc-01 sbin]# chmod a+x rsync.sh

[root@hyc-01 sbin]# sh -x rsync.sh

++ cat /usr/local/sbin/ip.list

+ for ip in '`cat /usr/local/sbin/ip.list`'

+ ./rsync.expect 192.168.31.129 /usr/local/sbin/file.list

spawn rsync -avR --files-from=/usr/local/sbin/file.list / root@192.168.31.129:/

root@192.168.31.129's password:

building file list ... done

root/

tmp/

 

sent 125 bytes  received 22 bytes  294.00 bytes/sec

total size is 26  speedup is 0.18

+ for ip in '`cat /usr/local/sbin/ip.list`'

+ ./rsync.expect 127.0.0.1 /usr/local/sbin/file.list

spawn rsync -avR --files-from=/usr/local/sbin/file.list / root@127.0.0.1:/

root@127.0.0.1's password:

building file list ... done

 

sent 119 bytes  received 12 bytes  87.33 bytes/sec

total size is 26  speedup is 0.20

[root@hyc-01 sbin]#

 

20.34 批量远程执行命令

 

[root@hyc-01 sbin]# vim exe.expect

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "hyc940421"

set cm [lindex $argv 1]

spawn ssh root@$host

expect {

"yes/no" { send "yes\r"}

"password:" { send "$passwd\r" }

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

 

[root@hyc-01 sbin]# vim exe.sh

#!/bin/bash

for ip in `cat /usr/local/sbin/ip.list`

do

  ./exe.expect $ip "hostname"

Done

 

执行:

[root@hyc-01 piliangzhixing]# chmod a+x exe.sh

[root@hyc-01 piliangzhixing]# chmod a+x exe.expect

[root@hyc-01 piliangzhixing]# sh exe.sh

spawn ssh root@192.168.31.129

root@192.168.31.129's password:

Last failed login: Thu Oct  4 21:05:10 CST 2018 from 192.168.31.128 on ssh:notty

There were 7 failed login attempts since the last successful login.

Last login: Thu Oct  4 18:23:24 2018 from 192.168.31.1

[root@hyc-01-01 ~]# hostname

hyc-01-01

[root@hyc-01-01 ~]# spawn ssh root@127.0.0.1

root@127.0.0.1's password:

Last failed login: Thu Oct  4 21:06:15 CST 2018 from localhost on ssh:notty

There were 5 failed login attempts since the last successful login.

Last login: Thu Oct  4 18:23:39 2018 from 192.168.31.1

[root@hyc-01 ~]# hostname

hyc-01

[root@hyc-01 ~]#

[root@hyc-01 piliangzhixing]#