20.31 expect脚本同步文件
自动同步文件 :
#!/usr/bin/expect
set passwd "123456"
spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
脚本解析:
spawn rsync -av root@192.168.133.132:/tmp/12.txt /tmp/
##以上是将/tmp/12.txt同步到本机/tmp/目录下;rsync同步
expect eof
##脚本执行完之后 ,再退出;如果没有这句 则直接突出,不会执行任何命令
执行效果:
[root@DasonCheng sbin]# rm -f /tmp/12.txt
[root@DasonCheng sbin]# ls /tmp/12.txt
ls: 无法访问/tmp/12.txt: 没有那个文件或目录
[root@DasonCheng sbin]# ./4.expect
spawn rsync -av root@192.168.60.12:/tmp/12.txt /tmp/
root@192.168.60.12'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@DasonCheng sbin]# !l
ls /tmp/12.txt
/tmp/12.txt
将expect eof注释掉:
[root@DasonCheng sbin]# !vim
vim 4.expect
[root@DasonCheng sbin]# !r
rm -f /tmp/12.txt
[root@DasonCheng sbin]# ./4.expect
spawn rsync -av root@192.168.60.12:/tmp/12.txt /tmp/
root@192.168.60.12's password: [root@DasonCheng sbin]# !l ##可能密码都没有输入就退出了,所以expect eof还是很有必要的;
ls /tmp/12.txt
ls: 无法访问/tmp/12.txt: 没有那个文件或目录
20.32 expect脚本指定host和要同步的文件
指定host和要同步的文件(从本地到远程地址):
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
set file2 [lindex $argv 2]
spawn rsync -av $file root@$host:$file2
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
执行结果(缺陷,只能传送一个文件;):
[root@DasonCheng sbin]# vim 5.expect
[root@DasonCheng sbin]# chmod +x 5.expect
[root@DasonCheng sbin]# ./5.expect 192.168.60.12 /tmp/12.txt /root/
spawn rsync -av /tmp/12.txt root@192.168.60.12:/root/
root@192.168.60.12's password:
sending incremental file list
12.txt
sent 79 bytes received 31 bytes 220.00 bytes/sec
total size is 5 speedup is 0.05
[root@DasonCheng sbin]#
20.33 构建文件分发系统
shell项目-分发系统-构建文件分发系统:
需求背景:
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。
实现思路:
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。
核心命令
rsync -av --files-from=list.txt / root@host:/
文件分发系统的实现:
- rsync.expect 内容
#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av --files-from=$file / root@$host:/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof
- list.txt内容
[root@DasonCheng sbin]# vim list.txt
/root/777
/etc/111/12.txt
- ip.txt内容
[root@DasonCheng sbin]# vim ip.txt
192.168.133.132
192.168.133.133
......
- rsync.sh 内容
#!/bin/bash
for ip in `cat ip.txt`
do
echo $ip
./rsync.expect $ip list.txt
done
文件分发系统内容解析:
rsync -avR --files-from=$file / root@$host:/
##这个为核心命令,$file指的是第二个参数list.txt文件集,都得用绝对路径;/指的是本机根目录、$host:/指的是远程根目录
##参数-R是在同步的时候,如果没有目录就会自动创建
执行效果:
[root@DasonCheng sbin]# sh -x rsync.sh
++ cat ip.txt
+ for ip in '`cat ip.txt`'
+ echo 192.168.60.12
192.168.60.12
+ cd /usr/local/sbin
+ ./rsync.expect 192.168.60.12 list.txt
spawn rsync -avR --files-from=list.txt / root@192.168.60.12:/
root@192.168.60.12's password:
building file list ... done
etc/
etc/111/
etc/111/12.txt
root/
root/777/
##以上效果已经实现,文件已经传达到了60.12;文件夹自动创建成功;
sent 145 bytes received 43 bytes 376.00 bytes/sec
total size is 0 speedup is 0.00
+ for ip in '`cat ip.txt`'
+ echo 127.0.0.1
127.0.0.1
+ cd /usr/local/sbin
+ ./rsync.expect 127.0.0.1 list.txt
spawn rsync -avR --files-from=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 b8:10:7b:2b:90:0a:d9:02:51:c9:57:33:81:52:ab:59.
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:
##可是,传送本地却报错了;我试着直接用rsync同步本地内容,也是同样报错;所以本地不可同步
重新执行上面的脚本!
[root@DasonCheng sbin]# sh -x rsync.sh
++ cat ip.txt
+ for ip in '`cat ip.txt`'
+ echo 192.168.60.12
192.168.60.12
+ cd /usr/local/sbin
+ ./rsync.expect 192.168.60.12 list.txt
spawn rsync -avR --files-from=list.txt / root@192.168.60.12:/
root@192.168.60.12's password:
building file list ... done
sent 94 bytes received 12 bytes 212.00 bytes/sec
total size is 0 speedup is 0.00
+ for ip in '`cat ip.txt`'
+ echo 127.0.0.1
127.0.0.1
+ cd /usr/local/sbin
+ ./rsync.expect 127.0.0.1 list.txt
spawn rsync -avR --files-from=list.txt / root@127.0.0.1:/
root@127.0.0.1's password:
protocol version mismatch -- is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(174) [sender=3.0.9]
##对本地是这样报错的;
shell环境直接执行:
[root@DasonCheng sbin]#
[root@DasonCheng sbin]# rsync -av /root/777 root@127.0.0.1:/tmp/777
root@127.0.0.1's password:
protocol version mismatch -- is your shell clean?
(see the rsync man page for an explanation)
rsync error: protocol incompatibility (code 2) at compat.c(174) [sender=3.0.9]
##本地执行报同样的错误!
[root@DasonCheng sbin]# rsync -av /root/777 root@192.168.60.12:/tmp/777
root@192.168.60.12's password:
sending incremental file list
777/
sent 39 bytes received 16 bytes 12.22 bytes/sec
total size is 0 speedup is 0.00
##同步到远程却可以!
20.34 批量远程执行命令
分发系统批量传送文件之后,来批量执行命令--这就类似于自动化
批量执行命令实现:
- 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"
- exe.sh 内容
#!/bin/bash
for ip in `cat ip.list`
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
注意:
expect脚本,文件名后缀以exp或者expect;
文件头必须以#!/usr/bin/expect 亲测,必须以这个开头; 需要给与chmod +x 执行权限;