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
操作过程
[root@linux-01 sbin]# vim 4.expect
写入上面 自动同步文件 内容
- [root@linux-01 sbin]# chmod a+x 4.expect
- [root@linux-01 sbin]# ./4.expect
- spawn rsync -av root@192.168.106.165:/tmp/12.txt /tmp/
- root@192.168.106.165's password:
receiving incremental file list
- 12.txt
- sent 30 bytes received 84 bytes 20.73 bytes/sec
- total size is 5 speedup is 0.04
20.32 expect 脚本指定 host 和要同步的文件
指定 host 和要同步的文件
- #!/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@linux-01 sbin]# vim 5.expect
写入上面的内容 指定 host 和要同步的文件
- [root@linux-01 sbin]# chmod a+x 5.expect
- [root@linux-01 sbin]# ./5.expect 192.168.106.165 /tmp/12.txt
- spawn rsync -av /tmp/12.txt root@192.168.106.165:/tmp/12.txt
- root@192.168.106.165's password:
sending incremental file list
- sent 31 bytes received 12 bytes 7.82 bytes/sec
- total size is 5 speedup is 0.12
- [root@linux-01 sbin]# ./5.expect 192.168.106.165 "/tmp/12.txt"
- spawn rsync -av /tmp/12.txt root@192.168.106.165:/tmp/12.txt
- root@192.168.106.165's password:
sending incremental file list
- sent 31 bytes received 12 bytes 7.82 bytes/sec
- total size is 5 speedup is 0.12
20.33 构建文件分发系统
需求背景
对于大公司而言, 肯定时不时会有网站或者配置文件更新, 而且使用的机器肯定也是好多台, 少则几台, 多则几十甚至上百台. 所以, 自动同步文件是至关重要的.
实现思路
首先要有一台模板机器, 把要分发的文件准备好, 然后只要使用 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
ip.list 内容
- 192.168.133.132
- 192.168.133.133
- ......
rsync.sh 内容
- #!/bin/bash
- for ip in cat /tmp/ip.list
- do
- ./rsync.expect $ip /tmp/file.list
- done
操作过程
- [root@linux-01 sbin]# 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@linux-01 sbin]# chmod a+x rsync.expect
- [root@linux-01 sbin]# vim /tmp/file.list
- /tmp/12.txt
- /root/shell/1.sh
- /root/111/222/123.txt
- [root@linux-01 sbin]# vim /tmp/ip.list
- 192.168.106.165
- [root@linux-01 sbin]# vim rsync.sh
- #!/bin/bash
- for ip in cat /tmp/ip.list
- do
- ./rsync.expect $ip /tmp/file.list
- done
- [root@linux-01 sbin]# sh -x rsync.sh
- ++ cat /tmp/ip.list
- + for ip in '`cat /tmp/ip.list`'
- + ./rsync.expect 192.168.106.165 /tmp/file.list
- spawn rsync -avR --files-from=/tmp/file.list / root@192.168.106.165:/
- root@192.168.106.165's password:
- building file list ... rsync: link_stat "/root/shell/1.sh" failed: No such file or directory (2)
- done
- root/
- root/111/
- root/111/222/
- root/111/222/123.txt
- tmp/
- tmp/12.txt
- sent 221 bytes received 62 bytes 43.54 bytes/sec
- total size is 5 speedup is 0.02
- rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9]
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
操作过程
[root@linux-01 sbin]# vim exe.expect
写入上面 exe.expect 内容
- [root@linux-01 sbin]# chmod a+x exe.expect
- [root@linux-01 sbin]# vim exe.sh
- #!/bin/bash
- for ip in cat /tmp/ip.list
- do
- echo $ip
- ./exe.expect $ip "w;free -m;ls /tmp"
- done
- [root@linux-01 sbin]# sh exe.sh
- 192.168.106.165
- spawn ssh root@192.168.106.165
- root@192.168.106.165's password:
- Last login: Tue Apr 24 17:47:27 2018 from 192.168.106.160
- [root@linux-02 ~]# w;free -m;ls /tmp
17:48:41 up 1:51, 2 users, load average: 0.00, 0.01, 0.05
USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
root pts/0 192.168.106.1 15:57 15:13 0.04s 0.04s -bash
root pts/1 192.168.106.160 17:48 1.00s 0.02s 0.01s w
- total used free shared buff/cache available
- Mem: 1822 159 1460 8 202 1473
- Swap: 4095 0 4095
12.txt systemd-private-419467cd2d3a46f89cde1d660ef24ce8-vgauthd.service-QH8NC0
d6z systemd-private-419467cd2d3a46f89cde1d660ef24ce8-vmtoolsd.service-fDRGcr
etc
来源: http://www.bubuko.com/infodetail-2634161.html