作者:oschina 来源:开源中国 时间:2018-05-16 20:55:52 我要评论

20.31 expect脚本同步文件

将文件从sever2同步到server1。

[[email protected] ~]# vim 4.expect
#!/usr/bin/expect
set passwd "123456"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yesr"}
"password:" { send "$passwdr" }
}
expect eof

更改权限:
[[email protected] ~]# chmod a+x 4.expect

执行:
[[email protected] ~]# ./4.expect 
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
[email protected]'s password: 
receiving incremental file list
12.txt

sent 30 bytes  received 84 bytes  76.00 bytes/sec
total size is 5  speedup is 0.04


检查本地文件:
[[email protected] ~]# ls /tmp/
12.txt 

说明: expect eof的作用是等待脚本中的命令执行完后再退出。


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

[[email protected] ~]# vim 5.expect
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file [email protected]$host:$file
expect {
"yes/no" { send "yesr"}
"password:" { send "$passwdr" }
}
expect eof

[[email protected] ~]# chmod a+x 5.expect

创建测试文件:
[[email protected] ~]# touch /tmp/3.txt

执行:
[[email protected] ~]# ./5.expect 192.168.8.138 "/tmp/3.txt"
spawn rsync -av /tmp/3.txt [email protected]:/tmp/3.txt
[email protected]'s password: 
sending incremental file list
3.txt

sent 69 bytes  received 31 bytes  200.00 bytes/sec
total size is 0  speedup is 0.00

客户端:  
[[email protected] ~]# ls /tmp/
12.txt  3.txt 

注: 本脚本只能同步一个文件。


20.33 构建文件分发系统

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

• 实现思路
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。

核心命令:

rsync -av --files-from=list.txt / [email protected]:/

创建 分发系统

创建一个文件列表文件备用:

[[email protected] ~]# vim /tmp/list.txt
/tmp/12.txt
/tmp/3.txt
#该文件下可以添加多个文件

注意:此处要保证客户端有同样的目录。

创建一个IP列表文件备用:

[[email protected] ~]# vim /tmp/iplist.txt
192.168.8.138
#该文件下可以指定多个IP

注意:此处IP(主机)密码要和rsync.expect脚本中一致。为了避免密码泄露的风险,可以使用密钥认证的方法。

创建rsync.expect脚本:

[[email protected] ~]# vim rsync.expect
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / [email protected]$host:/
expect {
"yes/no" { send "yesr"}
"password:" { send "$passwdr" }
}
expect eof

[[email protected] ~]# chmod a+x rsync.expect

创建 rsync.sh:

[[email protected] ~]# vim rsync.sh
#!/bin/bash
for ip in `cat /tmp/iplist.txt`
do
    ./rsync.expect $ip /tmp/list.txt
done

说明:该脚本的作用是遍历文件和IP列表。

执行:

[[email protected] ~]# sh -x rsync.sh
++ cat /tmp/iplist.txt
+ for ip in '`cat /tmp/iplist.txt`'
+ ./rsync.expect 192.168.8.138 /tmp/list.txt
spawn rsync -avR --files-from=/tmp/list.txt / [email protected]:/
[email protected]'s password: 
building file list ... done

sent 65 bytes  received 12 bytes  154.00 bytes/sec
total size is 0  speedup is 0.00

客户端:
[[email protected] ~]# ls /tmp/
12.txt  3.txt 

多个文件同步成功!


20.34 批量远程执行命令

创建exe.expect

[[email protected] ~]# vim exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "123456"
set cm [lindex $argv 1]
spawn ssh [email protected]$host
expect {
"yes/no" { send "yesr"}
"password:" { send "$passwdr" }
}
expect "]*"
send "$cmr"
expect "]*"
send "exitr"

[[email protected] ~]# chmod a+x exe.expect

该脚本的作用是远程执行命令。

创建exe.sh:

[[email protected] ~]# vim exe.sh
#!/bin/bash
for ip in `cat /tmp/iplist.txt`
do
  ./exe.expect $ip "hostname"
done

该脚本的作用是调用iplist.txt文件中的IP和exe.expect脚本。

执行

[[email protected] ~]# sh exe.sh 
spawn ssh [email protected]
[email protected]'s password: 
Last login: Thu Sep 21 18:21:42 2017 from 192.168.8.1
[[email protected] ~]# hostname
z2
[[email protected] ~]# [[email protected] ~]# 

常见问题:

1、rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]

我想请问这个错误有没影响呢?

答:有影响 说明没有完全同步完。

你再重复运行一次看看。

2、rsync.expect和rsync.sh都在sbin下面,而ip.list和file.list在tmp下面对吗

答:对的。 文件放到哪里都无所谓,只要你脚本中找到它即可。

扩展:
shell多线程 http://blog.lishiming.net/?p=448



文章转载自 开源中国社区 [http://www.oschina.net]

本文地址:https://my.oschina.net/u/3803405/blog/1813769

11阅读 | 0评论
你的回应
写文章

联系我们