1. 程式人生 > >shell程式設計(3)

shell程式設計(3)

shell專案——分發系統

對於大公司而言,肯定時不時會有網站或者配置檔案更新,而且使用的機器肯定也是好多臺,少則幾臺,多則幾十甚至上百臺。這樣的話一臺一臺配置肯定是不現實的,所以,自動同步檔案是至關重要的。

而要如何實現自動同步呢?首先要有一臺模板機器,把要分發的檔案準備好,然後只要使用expect指令碼批量把需要同步的檔案分發到目標機器即可。整個過程也稱為程式碼上線。  

expect指令碼遠端登入

  • 安裝expect:
[[email protected]:~]# yum install -y expect  //由於我們之前安裝過mkpasswd這個工具,這個工具會把expect一併安裝的。
  • 編輯自動遠端登入expect指令碼:
[[email protected] sbin]# vim login.expect          //寫入下面內容
#! /usr/bin/expect
set host "192.168.33.128"           //設定變數
set passwd "19940406"               //設定變數
spawn ssh [email protected]$host
expect {
"yes/no" { send "yes\r"; exp_continue }  //‘\r’表示回車鍵
"password:" { send "$passwd\r" }
}
interact            //表示停留在遠端機器上,而不需要退出,不加interact會退出

expect指令碼遠端登入,執行命令並退出

  • 編輯expect指令碼:
[[email protected] sbin]# vim login2.expect         //寫入下面內容
#! /usr/bin/expect
set user "root"
set passwd "19940406"
spawn ssh [email protected]

expect {
"yes/no" { send "yes\r"; exp_continue }
"password:" { send "$passwd\r" }
}
expect "]*"              //不管是root提示符還是普通使用者,都匹配到
send "touch /tmp/12.txt\r"         //執行命令
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"

expect指令碼傳遞引數

  • 編輯expect指令碼:
[[email protected] sbin]# vim login3.expect
#! /usr/bin/expect
set user [lindex $argv 0]            //第一個引數的值賦給user
set host [lindex $argv 1]            //第二個引數的值賦給host
set passwd "19940406"
set cm [lindex $argv 2]              //第三個引數的值賦給cm,表示命令
spawn ssh [email protected]$host
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"

expect指令碼同步檔案

  • 編輯expect指令碼:
[[email protected] sbin]# vim login4.expect              //寫入下面內容
#! /usr/bin/expect
set passwd "19940406"
spawn rsync -av [email protected]:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect eof             //表示expect指令碼結束,如果不加expect eof會導致還沒開始傳輸就會結束

如果想讓expect永遠不超時,可以在上面新增一行:

set timeout -1        //-1表示永不超時,設定為其它數字就會以其它數字為超時時間,以秒為單位,但是去掉expect eof 仍會出問題

expect指令碼指定host和要同步的檔案

[[email protected] sbin]# vim login5.expect               //寫入下面內容
#! /usr/bin/expect
set passwd "19940406"
set host [lindex $argv 0]
set file [lindex $argv 1]
spawn rsync -av $file [email protected]$host:$file
expect {
"yes/no" { send "$yes\r" }
"password:" { send "$passwd\r" }
}
expect eof

構建檔案分發系統

核心命令:
rsync -av --files-from=list.txt / [email protected]:/            //檔案路徑必須是絕對路徑

編輯expect指令碼:

[[email protected] sbin]# vim rsync.expect                 //寫入下面內容
#! /usr/bin/expect
set passwd "19940406"
set host [lindex $argv 0]
set file [lindex $argv 1]               //指的是list.txt ,表示一個列表而不僅僅是一個檔案
spawn rsync -avR --files-from=$file / [email protected]$host:/
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
export eof

編輯file.list:

[[email protected] sbin]# vim /tmp/file.list               //寫入下面內容
/tmp/12.txt
/tmp/1.txt
/tmp/2.txt
/tmp/3.txt
/tmp/111/111.txt

編輯ip.list:

[[email protected] sbin]# vim /tmp/ip.list                  //寫入下面內容
192.168.33.128
127.0.0.1

編輯 rsync.sh:

[[email protected] sbin]# vim rsync.sh                     //寫入下面內容
#! /bin/bash
for ip in `cat /tmp/ip.list`
do
        ./rsync.expect $ip /tmp/file.list
done

執行指令碼:

[[email protected] sbin]# chmod a+x rsync.expect
[[email protected] sbin]# sh rsync.sh 
spawn rsync -av --files-from=/tmp/file.list / [email protected]:/
[email protected]'s password: 
building file list ... done
tmp/
tmp/1.txt
tmp/2.txt
tmp/3.txt
tmp/111/
tmp/111/111.txt/

sent 311 bytes  received 82 bytes  262.00 bytes/sec
total size is 5  speedup is 0.01               //192.168.33.128傳輸成功
spawn rsync -av --files-from=/tmp/file.list / [email protected]:/
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.          //127.0.0.1出現問題,暫且不用管
ECDSA key fingerprint is SHA256:y/jrhxRLVYWO6YhN5a2DSQePmWH3CCVWcV578JDKbPg.
ECDSA key fingerprint is MD5:1b:86:bc:80:c5:c6:14:7a:4f:6c:29:b4:14:8f:86:d1.
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.

檢視192.168.33.128:

[[email protected] ~]# ls -l /tmp/
總用量 4
drwxr-xr-x 3 root  root  21 8月   1 14:47 111
-rw-r--r-- 1 root  root   5 8月   1 14:00 12.txt
-rw-r--r-- 1 root  root   0 8月   1 14:47 1.txt
-rw-r--r-- 1 root  root   0 8月   1 14:47 2.txt
-rw-r--r-- 1 root  root   0 8月   1 14:47 3.txt

分發系統命令批量執行

有時候我們在批量傳輸完檔案還不夠,還需要在目標機器上執行命令來操作一些服務,這時候就需要對機器進行命令的批量執行。

  • 編輯exe.expect:
[[email protected] sbin]# vim exe.expect                    //寫入下面內容
#! /usr/bin/expect
set host [lindex $argv 0]
set passwd "19940406"
set cm [lindex $argv 1]
spawn ssh [email protected]$host
expect {
"yes/no" { send "yes\r" }
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
  • 編輯exe.sh
[[email protected] sbin]# vim exe.sh                        //寫入下面內容
#! /bin/bash
for ip in `cat /tmp/ip.list`
do
        ./exe.expect $ip "hostname"
done
  • 執行指令碼:
[[email protected] sbin]# chmod a+x exe.expect 
[[email protected] sbin]# sh exe.sh 
spawn ssh [email protected]
[email protected]'s password: 
Last login: Wed Aug  1 15:06:08 2018 from 192.168.33.129
[[email protected] ~]# hostname
localhost.localdomain            //192.168.33.128主機名
[[email protected] ~]# spawn ssh [email protected]
[email protected]'s password: 
Last failed login: Wed Aug  1 15:17:30 CST 2018 from 127.0.0.1 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Wed Aug  1 13:54:06 2018 from 192.168.33.1
[[email protected] ~]# hostname
localhost.localdomain            //127.0.0.1本地主機名