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

20.31 expect脚本同步文件

使用expect脚本同步文件

说明:核心命令是rsync

#! /usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.2.116:/tmp/66.txt /tmp/
expect {
"yes/no" {send "yes\r"}
"password:" {send "$passwd\r"}
}
expect eof                       #最后一定要加expect eof 或interact,否则还没有执行完,就已经退出了

说明:把192.168.2.116机器上/tmp/66.txt同步到本机/tmp/目录下

#添加执行权限
[root@root-01 test]# chmod a+x 4.expect 

#执行
[root@root-01 test]# ./4.expect 
spawn rsync -av root@192.168.2.116:/tmp/66.txt /tmp/
root@192.168.2.116's password: 
receiving incremental file list
66.txt

sent 30 bytes  received 92 bytes  81.33 bytes/sec
total size is 13  speedup is 0.11

[root@root-01 test]# ls /tmp/66.txt 
/tmp/66.txt

说明:同步成功,本机/tmp/下有66.txt

 

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

本机文件同步到另外一台机器

说明:只支持同步单个文件

#! /usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]                 //host是第一个变量,(指定IP)
set file [lindex $argv 1]                 //file是第二个变量, (指定文件要写绝对路径)   
spawn rsync -av $file root@host:$file     //$file 是本机的文件到对方机器的文件
expect {
"yes/no" {send "yes\r"}
"password:" {send "$passwd\r"}
}
expect eof

说明:把本机的文件传输到另外一台机器


#添加执行权限
[root@root-01 test]# chmod a+x 5.expect


#执行
[root@root-01 test]# ./5.expect 192.168.2.116 "/tmp/1.log"
spawn rsync -av /tmp/1.log root@192.168.2.116:/tmp/1.log
root@192.168.2.116's password: 
sending incremental file list
1.log

sent 429 bytes  received 31 bytes  920.00 bytes/sec
total size is 356  speedup is 0.77

 

20.33 构建文件分发系统

需求背景 :

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

实现思路:

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

核心命令:

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

文件分发系统脚本

#! /usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]                               //host是第一个变量,(指定IP)
set file [lindex $argv 1]                               //file是第二个变量
spawn rsync -av --files-from=$file / root@$host:/      //$file 是本机的文件到对方机器的文件.
expect {
"yes/no" {send "yes\r"}
"password:" {send "$passwd\r"}
}
expect eof


说明:若不确定对方机器是否有相同的路径,那可以加 R (spawn rsync -avR --files-from=$file / root@$host:/  )
     加上R的话,即时对方机器没有相同的路径,也会自动创建.

#添加执行权限
[root@root-01 test]# chmod a+x 6.expect

 

定义list.txt文件列表

说明:list.txt文件列表,路径必须是绝对路径

[root@root-01 test]# vim /tmp/list.txt

/tmp/33.log
/root/2.txt
/root/test/ts/hh.txt

 

定义IP列表

说明:当上线的时候,需要同步的机器是不止一台,所以定义一个IP列表

[root@root-01 test]# vim /tmp/ip.txt


192.168.2.116
127.0.0.1

 

定义同步脚本

[root@root-01 test]# vim rsync.sh

#! /bin/bash
for ip in `cat /tmp/ip.txt`
do
    ./6.expect $ip /tmp/list.txt        //执行6.expect 后面跟第一个参数ip; 第二个参数是跟文件列表
done

 

执行rsync.sh

说明 :192.168.2.116这台机器同步成功,而127.0.0.1因为错误未能同步成功
           特别提示必须要保证所有同步的机器的密码一致.

[root@root-01 test]# sh -x rsync.sh 
++ cat /tmp/ip.txt
+ for ip in '`cat /tmp/ip.txt`'
+ ./6.expect 192.168.2.116 /tmp/list.txt
spawn rsync -avR --files-from=/tmp/list.txt / root@192.168.2.116:/
root@192.168.2.116's password: 
building file list ... done
root/
root/2.txt
root/test/
root/test/ts/
root/test/ts/hh.txt
tmp/
tmp/33.log

sent 770790 bytes  received 81 bytes  308348.40 bytes/sec
total size is 770407  speedup is 1.00
+ for ip in '`cat /tmp/ip.txt`'
+ ./6.expect 127.0.0.1 /tmp/list.txt
spawn rsync -avR --files-from=/tmp/list.txt / root@127.0.0.1:/
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.
ECDSA key fingerprint is bd:fd:56:cf:07:80:0d:a6:9c:38:00:be:33:1f:f5:7a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
root@127.0.0.1's password: [root@root-01 test]# 


#查看192.168.2.116机器上是否有/root/test/ts/hh.txt

[root@root-02 ~]# ls -l /root/test/ts/hh.txt
-rw-r--r-- 1 root root 0 9月  21 11:20 /root/test/ts/hh.txt

说明:是有/root/test/ts/hh.txt的,说明同步成功.

 

20.34 批量远程执行命令

说明:例如在传输文件之后,还需要做一些操作,重启Nginx服务或重启php服务

[root@root-01 test]# vim exe.expect

#! /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"


定义执行脚本

[root@root-01 test]# vim exe.sh

#! /bin/bash
for ip in `cat /tmp/ip.txt`
do
    ./exe.expect $ip "hostnamectl set-hostname root-03"     //执行./exe.expect 脚本 后面第一个参数Ip 和第二个参数执行的命令
done

 

执行exe.sh脚本

[root@root-01 test]# sh -x exe.sh
++ cat /tmp/ip.txt
+ for ip in '`cat /tmp/ip.txt`'
+ ./exe.expect 192.168.2.116 'hostnamectl set-hostname root-03'
spawn ssh root@192.168.2.116
root@192.168.2.116's password: 
Last login: Thu Sep 21 10:22:57 2017 from 192.168.2.102
[root@root-02 ~]# hostnamectl set-hostname root-03


 说明:执行的命令是设置hostname,可以看到原本是root-02,执行脚本后就设置成了root-03

 

转载于:https://my.oschina.net/AnnaWu/blog/1541066

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值