Shell编程(expect同步文件、指定host和同步文件、构建文件分发系统、批量执行命令)...

本文介绍如何使用expect脚本实现批量文件同步和远程命令执行,涵盖从单个文件同步到构建大规模文件分发系统的过程,以及批量执行远程命令的方法。

expect脚本同步文件

需求:自动同步文件 实验准备:

  • A机器:192.168.248.130
  • B机器:192.168.248.129 实现:

1.A机器编写4.expect脚本文件,内容如下所示:

#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.248.129:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}

expect eof #至关重要的一步设置,如果不加这条语句,那么还没有开始执行数据传输,就马上结束了,甚至有可能还没有远程登录成功,就已经退出来了,所以脚本里面必须要加这条语句。

2.执行权限

chmod a+x 4.expect

3.执行脚本

[root@yolks3 ~]# ./4.expect 
spawn rsync -av root@192.168.248.129:/tmp/12.txt /tmp/
couldn't execute "rsync": no such file or directory
    while executing
"spawn rsync -av root@192.168.248.129:/tmp/12.txt /tmp/"
    (file "./4.expect" line 3)

报错了,神奇的海螺。查询报错:couldn't execute "rsync",使用yum安装

yum -y install rsync

再次执行:已经ok,可以查看文件和大小

[root@yolks3 ~]# ./4.expect 
spawn rsync -av root@192.168.248.129:/tmp/12.txt /tmp/
root@192.168.248.129's password: 
receiving incremental file list
12.txt

sent 43 bytes  received 97 bytes  280.00 bytes/sec
total size is 5  speedup is 0.04
[root@yolks3 ~]# cat /tmp/12.txt 
1212
[root@yolks3 ~]# ls -lh /tmp/12.txt 
-rw-r--r-- 1 root root 5 9月  24 21:10 /tmp/12.txt

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

之前的3.expect文件默认是10秒钟超时,当然也是可以增加超时时间甚至可以让永久不超时。

只需要在脚本文件中添加第3行set timeout语句即可

expect "]*"
send "$cm\r"
set timeout 3  设置超时秒数,如果是-1表示永久不会超时
expect "]*"
send "exit\r"

需求:参数指定host和要同步的文件,这种方式只适合同步一个文件。 实现:

1.编写脚本5.expect文件,脚本内容如下所示:

#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0] #第一个变量是主机host(也就是主机IP地址)
set file [lindex $argv 1] #第二个变量是要同步的文件
spawn rsync -av $file root@$host:$file #这里是从本机到对方,而且file要写绝对路径。
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

2.执行权限

chmod a+x 5.expect

3.执行

[root@yolks3 ~]# ./5.expect 192.168.248.129 "/tmp/12.txt"
spawn rsync -av /tmp/12.txt root@192.168.248.129:/tmp/12.txt
root@192.168.248.129's password: 
sending incremental file list

sent 45 bytes  received 12 bytes  114.00 bytes/sec
total size is 5  speedup is 0.09

构建文件分发系统

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

实现思路:首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令:rsync -av --files-from=list.txt / root@host:/
实现:

1.编写 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:/ #如果不确定对方机器有相同的路径,可以加-avR
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

2.编写文件列表 /tmp/file.list,内容如下:

/tmp/12.txt
/tmp/yolks.file

3.编写IP地址列表文件 /tmp/ip.list

192.168.248.129
192.168.248.130

4.创建 rsync.sh shell文件

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

5.执行rsync.sh

1)执行权限

[root@yolks3 ~]# chmod a+x rsync.expect

2)执行shell脚本:rsync.sh

[root@yolks3 ~]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ ./rsync.expect 192.168.248.130 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.248.130:/
root@192.168.248.130's password: 
building file list ... done

sent 95 bytes  received 12 bytes  214.00 bytes/sec
total size is 5  speedup is 0.05
+ for ip in '`cat /tmp/ip.list`'
+ ./rsync.expect 192.168.248.129 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.248.129:/
root@192.168.248.129's password: 
building file list ... done
tmp/
tmp/yolks.file

sent 137 bytes  received 38 bytes  350.00 bytes/sec
total size is 5  speedup is 0.03

在192.168.248.129机器上,可以查看到刚才远程同步的2个文件。

[root@yolks2 ~]# ls -lt /tmp/12.txt /tmp/yolks.file 
-rw-r--r-- 1 root root 0 9月  26 00:15 /tmp/yolks.file
-rw-r--r-- 1 root root 5 9月  24 21:10 /tmp/12.txt

批量远程执行命令

想批量远程执行命令,可以通过2个脚本来实现。

1.创建 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"

2.添加执行权限

[root@yolks3 ~]# chmod a+x exe.expect

3.创建 exe.sh shell脚本

#!/bin/bash
 for ip in `cat /tmp/ip.list`
 do
     ./exe.expect $ip "hostname"
 done

4.执行

[root@yolks3 ~]# sh -x exe.sh 
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ ./exe.expect 192.168.248.130 hostname
spawn ssh root@192.168.248.130
root@192.168.248.130's password: 
Last failed login: Wed Sep 26 00:11:24 CST 2018 from 192.168.248.130 on ssh:notty
There were 5 failed login attempts since the last successful login.
Last login: Tue Sep 25 22:42:14 2018 from 192.168.248.1
[root@yolks3 ~]# hostname
yolks3
[root@yolks3 ~]# + for ip in '`cat /tmp/ip.list`'
+ ./exe.expect 192.168.248.129 hostname
spawn ssh root@192.168.248.129
root@192.168.248.129's password: 
Last failed login: Wed Sep 26 00:11:34 CST 2018 from 192.168.248.130 on ssh:notty
There were 3 failed login attempts since the last successful login.
Last login: Tue Sep 25 22:42:17 2018 from 192.168.248.1
[root@yolks2 ~]# hostname
yolks2
[root@yolks2 ~]# [root@yolks3 ~]# 

扩展

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

转载于:https://my.oschina.net/yolks/blog/2208670

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值