20.31 expect脚本同步文件
- 自动同步文件
#!/usr/bin/expect
set passwd "rootroot"
spawn rsync -av root@192.168.0.132:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r"}
}
expect eof
执行
[root@qingyun-01 sbin]# ./4.expect
spawn rsync -av root@192.168.0.132:/tmp/12.txt /tmp/
root@192.168.0.132's password:
receiving incremental file list
12.txt
sent 30 bytes received 84 bytes 228.00 bytes/sec
total size is 5 speedup is 0.04
[root@qingyun-01 sbin]# ls /tmp/
12.txt
20.32 expect脚本指定host和要同步的文件
- 指定host和要同步的文件
#脚本内容
#!/usr/bin/expect
set passwd "rootroot"
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@qingyun-01 sbin]# vim 5.expect
[root@qingyun-01 sbin]# chmod a+x 5.expect
[root@qingyun-01 sbin]# ./5.expect 192.168.0.132 "/tmp/12.txt"
spawn rsync -av /tmp/12.txt root@192.168.0.132:/tmp/12.txt
root@192.168.0.132's password:
sending incremental file list
sent 31 bytes received 12 bytes 86.00 bytes/sec
total size is 5 speedup is 0.12
#本脚本适合同步一个文件
#只把本地的12.txt同步到远程
20.33 构建文件分发系统
- 需求背景
对于大公司而言,肯定时不时会有网站或者配置文件更新,而且使用的机器肯定也是好多台,少则几台,多则几十甚至上百台。所以,自动同步文件是至关重要的。 - 实现思路
首先要有一台模板机器,把要分发的文件准备好,然后只要使用expect脚本批量把需要同步的文件分发到目标机器即可。 - 核心命令
rsync -av --files-from=list.txt / root@host:/
- 文件分发系统的实现
#rsync.expect内容
#!/usr/bin/expect
set passwd "rootroot"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -avR $file root@$host:$file
#如果不确定远程路径可以 加选项 -R
#来创建路径
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r"}
}
expect eof
#创建一个文件存放文件列表
[root@qingyun-01 sbin]# cat /tmp/file.list
/tmp/12.txt
/tmp/1/2/1.txt
/tmp/2/2.txt
- ip.list内容
[root@qingyun-01 sbin]# vim /tmp/ip.list
192.168.0.132
127.0.0.1
......
- rsync.sh 内容
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
echo $ip
./rsync.expect $ip /tmp/file.list
done
执行结果
[root@qingyun-01 sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ echo 192.168.0.132
192.168.0.132
+ ./rsync.expect 192.168.0.132 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.0.132:/
root@192.168.0.132's password:
building file list ... done
sent 143 bytes received 12 bytes 310.00 bytes/sec
total size is 5 speedup is 0.03
+ for ip in '`cat /tmp/ip.list`'
+ echo 127.0.0.1
127.0.0.1
+ ./rsync.expect 127.0.0.1 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@127.0.0.1:/
root@127.0.0.1's password:
building file list ... done
sent 143 bytes received 12 bytes 310.00 bytes/sec
total size is 5 speedup is 0.03
#注意:增加执行权限 chmod a+x 文件名
#结果
[root@qingyun-01 sbin]# ./1.expect
spawn ssh root@192.168.0.132
root@192.168.0.132's password:
Last login: Wed Feb 28 22:53:45 2018 from 192.168.0.130
[root@qingyun-02 ~]# ls /tmp/
1
12.txt
2
2.txt
[root@qingyun-02 ~]# ls /tmp/2/
2.txt
20.34 批量远程执行命令
- exe.expect 内容
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "rootroot"
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 /tmp/ip.list`
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done
执行过程&结果
[root@qingyun-01 sbin]# vi exe.expect
[root@qingyun-01 sbin]# vi exe.sh
[root@qingyun-01 sbin]# chmod a+x exe.expect ;chmod a+x exe.sh;
[root@qingyun-01 sbin]# sh -x exe.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ echo 192.168.0.132
192.168.0.132
+ ./exe.expect 192.168.0.132 'w;free -m;ls /tmp'
spawn ssh root@192.168.0.132
root@192.168.0.132's password:
Last login: Wed Feb 28 23:09:45 2018 from 192.168.0.130
[root@qingyun-02 ~]# w;free -m;ls /tmp
23:19:26 up 2:27, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.0.4 20:53 1:45m 0.06s 0.06s -bash
root pts/1 192.168.0.130 23:19 1.00s 0.01s 0.00s w
total used free shared buff/cache available
Mem: 1823 96 1465 8 261 1555
Swap: 2047 0 2047
1
12.txt
2
2.txt
systemd-private-f76127fe2f6b440a98fd57cb13173406-chronyd.service-lcHZr0
systemd-private-f76127fe2f6b440a98fd57cb13173406-vgauthd.service-HtFYwp
systemd-private-f76127fe2f6b440a98fd57cb13173406-vmtoolsd.service-rAX6FO
yum_save_tx.2018-02-28.20-58.xdulyV.yumtx
yum_save_tx.2018-02-28.21-03.8mtfU5.yumtx
[root@qingyun-02 ~]# + for ip in '`cat /tmp/ip.list`'
+ echo 127.0.0.1
127.0.0.1
+ ./exe.expect 127.0.0.1 'w;free -m;ls /tmp'
spawn ssh root@127.0.0.1
root@127.0.0.1's password:
Last failed login: Wed Feb 28 23:04:17 CST 2018 from 127.0.0.1 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Wed Feb 28 20:53:17 2018 from 192.168.0.4
[root@qingyun-01 ~]# w;free -m;ls /tmp
23:19:28 up 2:27, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.0.4 20:53 8.00s 0.42s 0.00s /usr/bin/expect ./exe.expect
root pts/2 127.0.0.1 23:19 0.00s 0.02s 0.00s w
total used free shared buff/cache available
Mem: 1823 99 1426 8 296 1550
Swap: 2047 0 2047
1 file.list
12.txt ip.list
1.txt systemd-private-2c7c3aa47d8f4e789cecfdb3a9fab54c-chronyd.service-2qG20X
2 systemd-private-2c7c3aa47d8f4e789cecfdb3a9fab54c-vgauthd.service-BmrrqI
2.txt systemd-private-2c7c3aa47d8f4e789cecfdb3a9fab54c-vmtoolsd.service-Pkezae
转载于:https://blog.51cto.com/3622288/2074098