20.31 expect脚本同步文件:
在expect脚本中去实现在一台机器上把文件同步到另外一台机器上去。核心命令用的是rsync
~1.自动同步文件
#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.208.130:/tmp/12.txt /tmp/ #把远程机器上的12.txt文件同步到本机上
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof #这里用到了eof,给他点时间让他传输文件(或用interact停留在远程的机器上)
实例:
[root@axinlinux-01 sbin]# ./4.expect
spawn rsync -av root@192.168.208.130:/tmp/12.txt /tmp/
root@192.168.208.130's password:
receiving incremental file list
12.txt
sent 43 bytes received 97 bytes 93.33 bytes/sec
total size is 5 speedup is 0.04
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20.32 expect脚本指定host和要同步的文件:
~1.指定host和要同步的文件
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0] #第一个参数是host(对方的主机,是传到对方的)
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@axinlinux-01 sbin]# ./5.expect 192.168.208.130 "/tmp/123.txt"
spawn rsync -av /tmp/123.txt root@192.168.208.130:/tmp/123.txt
root@192.168.208.130's password:
sending incremental file list
123.txt
sent 88 bytes received 35 bytes 246.00 bytes/sec
total size is 0 speedup is 0.00
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20.33 构建文件分发系统:
我们需要把一堆文件写入到文件列表里面去,而不是一个文件
~1.需求背景:
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
~2.实现思路:
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
~3.核心命令:
rsync -av --files-from=list.txt / root@host:/
list.txt就是我们的文件列表,这里的路径要是绝对路径
~4.文件分发系统的实现:(传输多个文件)
~4.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:/ #这里的源目录是根,目标目录也是根。如果不能保证对方机器上有相同的路径,就要加上-R(会自动创建)
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
~4.2 list.txt内容(这里的路径要保证对方机器上也有这个路径,要传输的文件有没有无所谓。不能保证的话要在脚本rsync命令加上-R)
/tmp/123.txt
/root/shell/1.sh
/root/11.txt
~.4.3 ip.list内容(因为远程同步文件的机器不止一台,所以要加上要同步的机器的IP)
192.168.133.132
192.168.133.133
......
做expect脚本的前提是要保证要传输的机器的密码要是一样的。如果不一样的话,只能挨个定义每一台机器的密码,但是不安全。当然也可以做秘钥认证,那脚本输密码的那一行就可以省略"password:" { send "$passwd\r" }
~.4.4 rsync.sh 内容(遍历一下IP地址)执行的时候就是执行rsync.sh
#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./rsync.expect $ip list.txt
done
实例:
[root@axinlinux-01 sbin]# vim rsync.expect
#!/usr/bin/expect
set passwd "wangxin789" #密码在测试中两个都是一样的
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR --files-from=$file / root@$host:/ #加上-R保证目录不存在的时候会创建
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
[root@axinlinux-01 sbin]# vim list.txt
/root/rsync.test/1.txt
/root/rsync2.test/2.txt
[root@axinlinux-01 sbin]# vim ip.list
192.168.208.130
127.0.0.1
[root@axinlinux-01 sbin]# sh -x rsync.sh
[root@axinlinux-02 ~]# ls -l /root/rsync.test/1.txt #在02机器上检查一下是否传输过去,是否创建不存在的目录
-rw-r--r-- 1 root root 0 9月 23 22:05 /root/rsync.test/1.txt
[root@axinlinux-02 ~]# ls -l /root/rsync2.test/2.txt
-rw-r--r-- 1 root root 0 9月 23 22:06 /root/rsync2.test/2.txt
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20.34 批量远程执行命令:
以上我们完成了分发文件,但是还不够。有时候我们需要执行一些命令,比如重启nginx或php。类似自动化了,批量一些执行命令、批量传一些文件
以下是,批量执行一些命令的脚本
~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.exe.sh 内容 (还要定义exe.sh的脚本,也就是for循环的。也就是我们还要遍历要同步数据的机器的IP,因为上一节中已经创建了的那个IP脚本)
#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
实例:
[root@axinlinux-01 sbin]# vim exe.expect
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "wangxin789"
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@axinlinux-01 sbin]# vim exe.sh
#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
[root@axinlinux-01 sbin]# chmod a+x exe.sh
[root@axinlinux-01 sbin]# chmod a+x exe.expect
[root@axinlinux-01 sbin]# sh -x exe.sh