1. 程式人生 > >expect腳本同步文件、expect腳本指定host和要同步的文件、構建文件分發系統、批量遠程執行

expect腳本同步文件、expect腳本指定host和要同步的文件、構建文件分發系統、批量遠程執行

expect腳本同步文件 expect腳本指定host和要同步的 構建文件分發系統 批量遠程執行命令

20.31 expect腳本同步文件
  • 自動同步文件
#!/usr/bin/expect
set passwd "rootroot"
spawn rsync -av [email protected]:/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 [email protected]:/tmp/12.txt /tmp/
[email protected]‘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 [email protected]:/tmp/12.txt
[email protected]‘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 / [email protected]:/
[email protected]‘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 / [email protected]:/
[email protected]‘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 [email protected]
[email protected]‘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 [email protected]
[email protected]‘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 [email protected]
[email protected]‘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

expect腳本同步文件、expect腳本指定host和要同步的文件、構建文件分發系統、批量遠程執行