expect 構建分發文件系統

20.31 expect腳本同步文件

將文件從sever2同步到server1。

[root@z1 ~]# vim 4.expect
#!/usr/bin/expect
set passwd "123456"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

更改權限:
[root@z1 ~]# chmod a+x 4.expect

執行:
[root@z1 ~]# ./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 76.00 bytes/sec
total size is 5 speedup is 0.04

檢查本地文件:
[root@z1 ~]# ls /tmp/
12.txt
說明: expect eof的作用是等待腳本中的命令執行完後再退出。

20.32 expect腳本指定host和要同步的文件

[root@z1 ~]# vim 5.expect
#!/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@z1 ~]# chmod a+x 5.expect

創建測試文件:
[root@z1 ~]# touch /tmp/3.txt

執行:
[root@z1 ~]# ./5.expect 192.168.8.138 "/tmp/3.txt"
spawn rsync -av /tmp/3.txt [email protected]:/tmp/3.txt
[email protected]'s password:
sending incremental file list
3.txt

sent 69 bytes received 31 bytes 200.00 bytes/sec
total size is 0 speedup is 0.00

客戶端:
[root@z2 ~]# ls /tmp/
12.txt 3.txt
注: 本腳本只能同步一個文件。

20.33 構建文件分發系統

需求背景:
對於大公司而言,肯定時不時會有網站或者配置文件更新,而且使用的機器肯定也是好多臺,少則幾臺,多則幾十甚至上百臺。所以,自動同步文件是至關重要的。

實現思路:
首先要有一臺模板機器,把要分發的文件準備好,然後只要使用expect腳本批量把需要同步的文件分發到目標機器即可(把多個文件分發到多臺機器時需要創建文件、IP列表,即本文中的list.txt、iplist.txt)。

核心命令:

rsync -av --files-from=list.txt / root@host:/
創建 分發系統

創建一個文件列表文件備用:

[root@z1 ~]# vim /tmp/list.txt
/tmp/12.txt
/tmp/3.txt
#該文件下可以添加多個文件
注意:此處要保證客戶端有同樣的目錄。

創建一個IP列表文件備用:

[root@z1 ~]# vim /tmp/iplist.txt
192.168.8.138
#該文件下可以指定多個IP
注意:此處IP(主機)密碼要和rsync.expect腳本中一致。爲了避免密碼泄露的風險,可以使用密鑰認證的方法。

創建rsync.expect腳本:

[root@z1 ~]# 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@z1 ~]# chmod a+x rsync.expect
創建 rsync.sh:

[root@z1 ~]# vim rsync.sh
#!/bin/bash
for ip in cat /tmp/iplist.txt
do
./rsync.expect $ip /tmp/list.txt
done
說明:該腳本的作用是遍歷文件和IP列表。

執行:

[root@z1 ~]# sh -x rsync.sh
++ cat /tmp/iplist.txt

  • for ip in 'cat /tmp/iplist.txt'
  • ./rsync.expect 192.168.8.138 /tmp/list.txt
    spawn rsync -avR --files-from=/tmp/list.txt / [email protected]:/
    [email protected]'s password:
    building file list ... done

sent 65 bytes received 12 bytes 154.00 bytes/sec
total size is 0 speedup is 0.00

客戶端:
[root@z2 ~]# ls /tmp/
12.txt 3.txt
多個文件同步成功!

20.34 批量遠程執行命令

創建exe.expect

[root@z1 ~]# vim 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"

[root@z1 ~]# chmod a+x exe.expect
該腳本的作用是遠程執行命令。

創建exe.sh:

[root@z1 ~]# vim exe.sh
#!/bin/bash
for ip in cat /tmp/iplist.txt
do
./exe.expect $ip "hostname"
done
該腳本的作用是調用iplist.txt文件中的IP和exe.expect腳本。

執行

[root@z1 ~]# sh exe.sh
spawn ssh [email protected]
[email protected]'s password:
Last login: Thu Sep 21 18:21:42 2017 from 192.168.8.1
[root@z2 ~]# hostname
z2
[root@z2 ~]# [root@z1 ~]#

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章