expect 构建分发文件系统

20.31 expect脚本同步文件

将文件从sever2同步到server1。

[root@z1 ~]# vim 4.expect
#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.8.138:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

更改权限:
[root@z1 ~]# chmod a+x 4.expect

执行:
[root@z1 ~]# ./4.expect 
spawn rsync -av root@192.168.8.138:/tmp/12.txt /tmp/
root@192.168.8.138'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


检查本地文件:
[root@z1 ~]# ls /tmp/
12.txt 

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

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

[root@z1 ~]# vim 5.expect
#!/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@z1 ~]# chmod a+x 5.expect

创建测试文件:
[root@z1 ~]# touch /tmp/3.txt

执行:
[root@z1 ~]# ./5.expect 192.168.8.138 "/tmp/3.txt"
spawn rsync -av /tmp/3.txt root@192.168.8.138:/tmp/3.txt
root@192.168.8.138'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

客户端:  
[root@z2 ~]# ls /tmp/
12.txt  3.txt 

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

20.33 构建文件分发系统

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

实现思路:
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可(把多个文件分发到多台机器时需要创建文件、IP列表,即本文中的list.txt、iplist.txt)。

核心命令:

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

创建 分发系统

创建一个文件列表文件备用:
[root@z1 ~]# vim /tmp/list.txt
/tmp/12.txt
/tmp/3.txt
#该文件下可以添加多个文件

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

创建一个IP列表文件备用:
[root@z1 ~]# vim /tmp/iplist.txt
192.168.8.138
#该文件下可以指定多个IP

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

创建rsync.expect脚本:

[root@z1 ~]# 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 / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

[root@z1 ~]# chmod a+x rsync.expect
创建 rsync.sh:
[root@z1 ~]# vim rsync.sh
#!/bin/bash
for ip in `cat /tmp/iplist.txt`
do
    ./rsync.expect $ip /tmp/list.txt
done

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

执行:
[root@z1 ~]# 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 / root@192.168.8.138:/
root@192.168.8.138's password: 
building file list ... done

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

客户端:
[root@z2 ~]# ls /tmp/
12.txt  3.txt 

多个文件同步成功!

20.34 批量远程执行命令

创建exe.expect

[root@z1 ~]# 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@z1 ~]# chmod a+x exe.expect

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

创建exe.sh:

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

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

执行

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

转载于:https://my.oschina.net/adailinux/blog/1541341

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值