筆記內容:java
筆記日期:2017-11-29mysql
<br>web
所謂分發系統就是一個主要用來上線代碼或同步文件的腳本,先來看一下需求背景:sql
因此分發系統就是用來完成以上這個需求的,分發系統須要完成的事情就是將須要上線的代碼分發到這些線上服務器中。咱們如今要作的就是實現這個分發系統,實現這個系統須要用到兩個主要的東西就是shell和expect,經過shell結合expect能夠編寫一個簡單的分發系統。shell
<br>vim
文件分發系統的實現:
1.使用expect編寫一個腳本文件rsync.expect,這個腳本是實現文件同步的腳本,內容以下:後端
[root@localhost ~/expectFiles]# 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@localhost ~/expectFiles]# chmod a+x rsync.expect
2.而後再編輯一個文本文件,這個文件用來放須要同步的文件列表,例如我這裏隨便同步幾個文件:bash
[root@localhost ~/expectFiles]# vim /tmp/list.txt /tmp/12.txt /root/test.sh /tmp/test.txt /root/expectFiles/hostFile.expect
提示:若是你的rsync命令沒有加-R選項的話,就須要確保目標機器也有這個文件列表中所定義的目錄路徑,否則就會報錯。服務器
3.還須要編輯一個ip.list,用於存放須要同步的目標機器的IP地址,例如我須要將文件都同步這幾個IP的機器上:ssh
[root@localhost ~/expectFiles]# vim /tmp/ip.list 192.168.77.128 192.168.77.130
須要同步的目標機器的密碼最好是一致的,由於只是作實驗爲了簡單化就不使用密鑰驗證了。
4.再編寫一個shell腳本rsync.sh,這個腳本比較簡單,只是遍歷出ip.list文件內容而後交給rsync.expect腳本去執行而已,示例:
[root@localhost ~/expectFiles]# vim rsync.sh #!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip # 第二個參數就是須要同步的文件列表 ./rsync.expect $ip /tmp/list.txt done
最後咱們只須要執行rsync.sh腳本便可:
[root@localhost ~/expectFiles]# sh rsync.sh 192.168.77.128 spawn rsync -avR --files-from=/tmp/list.txt / root@192.168.77.128:/ root@192.168.77.128's password: building file list ... done root/ root/test.sh root/expectFiles/ root/expectFiles/hostFile.expect tmp/ tmp/12.txt tmp/test.txt sent 666 bytes received 97 bytes 1526.00 bytes/sec total size is 288 speedup is 0.38 192.168.77.130 spawn rsync -avR --files-from=/tmp/list.txt / root@192.168.77.130.83:/ root@192.168.77.130's password: building file list ... done root/ root/test.sh root/expectFiles/ root/expectFiles/hostFile.expect tmp/ tmp/12.txt tmp/test.txt sent 666 bytes received 97 bytes 1526.00 bytes/sec total size is 288 speedup is 0.38 [root@localhost ~/expectFiles]#
運行結果如上,沒有報錯,文件也正常同步了,這樣咱們就實現了一個很簡單的文件分發系統。
<br>
以上咱們已經實現了簡單的文件分發系統,可是光只能同步文件還不夠。由於假設這是網站的後端代碼文件,同步完了以後須要重啓web服務或者執行一些命令,因此還得再編寫一個可以批量遠程執行命令的腳本。
1.一樣的先使用expect編寫遠程登陸的腳本文件exe.expect, 內容以下:
[root@localhost ~/expectFiles]# 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@localhost ~/expectFiles]# chmod a+x exe.expect
2.除此以外還須要寫一個shell腳本exe.sh,這個腳本和rsync.sh差很少都是遍歷出ip.list文件裏的ip,內容以下:
[root@localhost ~/expectFiles]# vim exe.sh #!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip # 第二個參數就是須要執行的命令 ./exe.expect $ip "who;ls" done
執行exe.sh腳本:
[root@localhost ~/expectFiles]# sh exe.sh 192.168.77.128 spawn ssh root@192.168.77.128 root@192.168.77.128's password: Last login: Wed Nov 29 21:56:41 2017 from 192.168.77.130 [root@localhost ~]# who;ls root pts/0 2017-11-29 21:26 (192.168.77.1) root pts/1 2017-11-29 21:57 (192.168.77.130) \ mysql57-community-release-el7-8.noarch.rpm Test.java anaconda-ks.cfg stop.sh test.sh expectFiles Test.class zabbix-release-3.2-1.el7.noarch.rpm [root@localhost ~]# 192.168.77.130 spawn ssh root@192.168.77.130 root@192.168.77.130's password: Last login: Thu Nov 30 05:49:18 2017 from 192.168.77.130 [root@localhost ~]# who;ls root tty1 2017-11-30 05:23 root pts/0 2017-11-30 03:33 (192.168.77.1) root pts/2 2017-11-30 05:50 (192.168.77.130) accept_local~ expectFiles grep logs sed shellFile Test test.sh [root@localhost ~]# [root@localhost ~/expectFiles]#
運行結果如上,沒毛病。
done:至此簡易的分發系統和批量遠程執行命令的功能就完成了。