expect同步文件&expect指定host和要同步的文件&构建文件分发系统&批量远程执行命令...

20.31 expect脚本同步文件

expect通过与rsync结合,可以在一台机器上把文件自动同步到多台机器上

编写脚本

[root@linux-5 ~]# cd /usr/local/sbin
[root@linux-5 sbin]# vim 4.expect
#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.88.10:/root/1.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

注:expect eof的作用,可以为spwan所执行的命令提供足够的时间执行,不会马上退出expect,从而导致命令尚未执行完毕而退出的情况(尤其是文件传输的命令);interact的作用可以使远程登录后保留登录状态

脚本授权

[root@linux-5 sbin]# chmod a+x !$
chmod a+x ./4.expect

执行脚本

[root@linux-5 sbin]# ./4.expect 
spawn rsync -av root@192.168.88.10:/root/1.txt /tmp/
receiving incremental file list
1.txt

sent 43 bytes  received 96 bytes  278.00 bytes/sec
total size is 6  speedup is 0.04
expect: spawn id exp6 not open
    while executing
"expect eof"
    (file "./4.expect" line 8)

结果验证

[root@linux-5 sbin]# cat /tmp/1.txt
12345

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

通过带参数的方式为指定的地址同步指定的文件

编写脚本

#!/usr/bin/expect
set passwd "123456"
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@linux-5 sbin]# chmod a+x 5.expect

执行脚本

[root@linux-5 sbin]# ./5.expect 192.168.88.10 /tmp/1.txt 
spawn rsync -av /tmp/1.txt root@192.168.88.10:/tmp/1.txt
sending incremental file list
1.txt

sent 96 bytes  received 35 bytes  262.00 bytes/sec
total size is 6  speedup is 0.05
expect: spawn id exp6 not open
    while executing
"expect eof"
    (file "./5.expect" line 10)

结果验证

[root@linux-10 ~]# cat /tmp/1.txt 
12345

20.33 构建文件分发系统

需求背景

对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。

实现思路

首先要有一台模板机器,把要分发的文件准备好,编写两个脚本,一个expect文件分发脚本,一个IP遍历脚本,将分发脚本嵌套在IP遍历脚本中,用IP遍历脚本为文件分发脚本提供IP参数以及需要同步的文件目录,再使用expect文件分发脚本批量把需要同步的文件分发到目标机器即可实现。

核心命令

rsync -av --files-from=list.txt / root@host:/

使用rsync 的 --files参数,可以实现调用文件里面的列表,进行多个文件远程传输,进而实现文件分发

构建文件分发系统

编写expect文件分发脚本

#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / root@$host:/   ##定义了原目录和目标目录以根目录开始
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

同步的路径,需要保证对方机器也有这个相同的路径,如果没有路径,需要使用 -R 选项创建路径

编写IP遍历脚本

#!/bin/bash 
for ip in `cat /tmp/ip.list`
do
    echo $ip   
    ./rsync.expect $ip /tmp/file.list
done

创建需要同步的文件的列表文件

vim /tmp/file.list
##将需要同步的文件的绝对路径写入列表文件中
/usr/local/sbin/1.sh
/usr/local/sbin/2.sh
/usr/local/sbin/3.sh
/root/23.txt

创建需要同步的IP地址的列表文件

vim /tmp/ip.list
192.168.88.10

脚本授权

[root@linux-5 sbin]# chmod a+x rsync.expect
[root@linux-5 sbin]# chmod a+x rsync.sh

脚本测试

[root@linux-5 sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ echo 192.168.88.10
192.168.88.10
+ ./rsync.expect 192.168.88.10 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.88.10:/
building file list ... done
root/
root/23.txt
usr/local/
usr/local/sbin/
usr/local/sbin/01.sh
usr/local/sbin/02.sh
usr/local/sbin/03.sh

sent 647 bytes  received 101 bytes  1,496.00 bytes/sec
total size is 271  speedup is 0.36
expect: spawn id exp6 not open
    while executing
"expect eof"
    (file "./rsync.expect" line 10)

注:分发系统还有一个重要的关键是,确保同步的机器的密码一致,否则将不能实现同步;所以这就存在一个弊端,一旦脚本暴露,将会让别人知道如何登陆你机器;当然也有对应的解决办法,那就是使用密钥认证,这样的话,自然在命令行业省去“输入密码< password:" { send "$passwd\r" } >''”和“定义密码< set passwd "123456" >”的命令了

20.34 批量远程执行命令

实现原理

与批量分发类似,需要将远程执行命令脚本嵌套在IP遍历脚本中,由IP遍历脚本为远程执行命令脚本传递IP的参数和所需要执行的具体命令的参数。

编写远程执行命令脚本

vim exe.except
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
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"

编写IP遍历脚本

vim exe.sh
##利用for循环遍历IP,传递给远程脚本相应参数,并在循环中执行远程脚本
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
   ./exe.except $ip "w;ls"
done

脚本授权

[root@linux-5 sbin]# chmod a+x exe.except
[root@linux-5 sbin]# chmod a+x exe.sh

脚本测试

[root@linux-5 sbin]# sh -x exe.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ ./exe.except 192.168.88.10 'w;ls'
spawn ssh root@192.168.88.10
Last login: Sat Jul 21 16:37:14 2018 from 192.168.88.5
[root@linux-10 ~]# w;ls
 16:45:52 up  2:18,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.88.1     15:47   52:08   0.00s  0.00s -bash
root     pts/1    192.168.88.5     16:45    0.00s  0.00s  0.00s w
1.txt  23.txt  anaconda-ks.cfg  zabbix-release-3.2-1.el7.noarch.rpm

 

转载于:https://my.oschina.net/u/3804357/blog/1860414

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值