Shell項目之分發系統-expect(下)

[toc]php

5、自動同步文件

5.1 配置腳本

核心命令,就是用的rsync服務。mysql

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

5.2 受權並測試

# chmod a+x 4.expect 

[root@xavi sbin]# ./4.expect
spawn rsync -avR  //加上-R是爲了便於在鏈接的主機上沒有相同文件路徑時,自動建立
root@116.62.XXX.XXX:/tmp/12.txt /tmp/
root@116.62.XXX.XXX's password: 
receiving incremental file list
tmp/
tmp/12.txt

sent 34 bytes  received 115 bytes  298.00 bytes/sec
total size is 5  speedup is 0.03

如上能夠看出 已經成功的同步了文件。linux

expect eof 的做用

腳本中,若是咱們不加此語句,一旦登陸成功就會退出。nginx

在這裏加入set timeout替代expect eof是無效的

6、指定host和要同步的文件

6.1 腳本配置

vim 5.expect

#!/usr/bin/expect
set passwd "123456"
set host [lindex $argv 0]  //第一個參數是host,ip
set file [lindex $argv 1]  //第二個參數是文件,file是絕對路徑
spawn rsync -av $file root@$host:$file
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect eof

腳本目的是:推送一個本地文件到遠程服務器。sql

6.2 受權

# chmod a+x 5.expect

我如今嘗試把我本地的一個文件推送到服務器:文件內容以下:shell

# cat /tmp/users.txt 
username: user_9 password: 1zg^NIx7on
username: user_5 password: e21oHjeYg=
username: user_6 password: e21oHjeYg=
username: user_7 password: e21oHjeYg=
username: user_8 password: e21oHjeYg=
username: user_5 password: gNR0o{1qjw
username: user_6 password: Pam1lxsN6[
username: user_7 password: 6hd5p^XgwM
username: user_8 password: kvcoyJ5_G0
username: user_5 password: is0wb*SNj7
username: user_6 password: BEgg89qgz<
username: user_7 password: Nxvt8-xGw8
username: user_8 password: 6mpbk5sDS-

6.3 執行,腳本+host地址+文件(絕對路徑)

# ./5.expect 192.168.XXX.XXX "/tmp/users.txt"  

spawn rsync -av /tmp/users.txt root@192.168.72.132:/tmp/users.txt
root@192.168.72.132's password: 
sending incremental file list
users.txt

sent 571 bytes  received 31 bytes  1204.00 bytes/sec
total size is 494  speedup is 0.82

6.4 去遠程服務器上查看

[root@xavi ~]# cat /tmp/users.txt 
username: user_9 password: 1zg^NIx7on
username: user_5 password: e21oHjeYg=
username: user_6 password: e21oHjeYg=
username: user_7 password: e21oHjeYg=
username: user_8 password: e21oHjeYg=
username: user_5 password: gNR0o{1qjw
username: user_6 password: Pam1lxsN6[
username: user_7 password: 6hd5p^XgwM
username: user_8 password: kvcoyJ5_G0
username: user_5 password: is0wb*SNj7
username: user_6 password: BEgg89qgz<
username: user_7 password: Nxvt8-xGw8
username: user_8 password: 6mpbk5sDS-

已經成功推送!!!vim

可是這只是開始,通常咱們推送一個軟件或者上線一個服務確定不是一個文件就能夠的。因此咱們須要的是大量的文件子目錄+腳本推送。bash

7、文件分發系統實現

7.1 需求背景

  • [ ] 對於大公司而言,常常會有網站或者配置文件更新,並且使用的機器確定也是好多臺,少則幾臺,多則幾十甚至上百臺。因此,自動同步文件是相當重要的。
    實現思路:服務器

  • [ ] 首先要有一臺模板機器,把要分發的文件準備好,而後只要使用expect腳本批量把須要同步的文件分發到目標機器便可。

7.2 核心命令:rsync -av --files-from=list.txt / root@host:/

注意這裏的都是根目錄ssh

7.3 腳本配置

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

核心命令:
rsync -av –files-from=$file / root@$host:/:上傳文件的列表是$file,咱們須要在list當中去定義 哪些目錄或文件是咱們須要同步更新上線的。

7.4 文件列表

[root@xavi sbin]# !vim
vim /tmp/list.txt

/tmp/12.txt
/root/for01.sh
/root/temp/test.txt
  • [ ] 假如咱們須要同步如上3個文件,前提是必定要保證對方的server必定也要有這個目錄,例如: /tmp/ /user/local/sbin/ /tmp/, 否則同步會出錯。(如上腳本中咱們添加了 rsync -avR R就是自動建立級聯的目錄。)

7.5 IP列表

[root@xavi sbin]# vim /tmp/ip.list

192.168.72.133
127.0.0.1

有一個重要的前提就是:全部的機器必需要保證有着相同的密碼。可是這樣操做有點危險,一旦文檔泄露就比較麻煩了。因此咱們會選擇針對須要同步的服務器進行密鑰認證。一旦密鑰認證後,以下腳本中的語句咱們就能夠省略:

"password:" { send "$passwd\r" }

第二種方法就是:逐一修改密碼

[root@xavi sbin]# passwd
更改用戶 root 的密碼 。
新的 密碼:
從新輸入新的 密碼:

passwd:全部的身份驗證令牌已經成功更新。

7.6 建立一個rsync的shell腳本

腳本的目的:遍歷全部的server和list中的文件能夠同步到每臺服務器。

[root@xavi sbin]# vim rsync.sh

#!/bin/bash

for ip in `cat /tmp/ip.list`
do
    echo $ip
    ./rsync.expect $ip /tmp/list.txt
done

7.7 受權並測試:

[root@xavi sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ echo 116.62.XXX.XXX
116.62.XXX.XXX
+ ./rsync.expect 116.62.XXX.XXX /tmp/list.txt
spawn rsync -avR --files-from=/tmp/list.txt / root@116.62.XXX.XXX:/
root@116.62.XXX.XXX's password: 
building file list ... done
root/
root/for01.sh
root/temp/
root/temp/test.txt
tmp/
tmp/12.txt

sent 622 bytes  received 84 bytes  1412.00 bytes/sec
total size is 339  speedup is 0.48
+ for ip in '`cat /tmp/ip.list`'
+ echo 127.0.0.1
127.0.0.1
+ ./rsync.expect 127.0.0.1 /tmp/list.txt
spawn rsync -avR --files-from=/tmp/list.txt / root@127.0.0.1:/
root@127.0.0.1's password: 
building file list ... done

sent 145 bytes  received 12 bytes  314.00 bytes/sec
total size is 339  speedup is 2.16

8、批量遠程執行命令

有時候當咱們傳輸同步完畢以後,可能須要重啓一下nginx服務,或者mysql服務,這樣就用到了遠程執行命令。

8.1 腳本配置

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 "]*"
  • [ ] set host [lindex $argv 0]:第一個參數

  • [ ] set cm [lindex $argv 1]:第二個參數

8.2 expect腳本受權

chmod a+x exe.expect

8.3 定義exe的shell腳本

[root@xavi sbin]# vim exe.sh

#!/bin/bash

for ip in `cat ip.list`
do
echo $ip
./exe.expect $ip "w;free -m;ls /tmp"
done

mark

8.4 登錄到了定義的遠程server,而且執行了相關的命令!

[root@xavi sbin]# sh -x exe.sh
++ cat ip.list
+ for ip in '`cat ip.list`'
+ echo 116.62.xxx.xxx
116.62.xxx.xxx
+ ./exe.expect 116.62.xxx.xxx 'w;free -m;ls /tmp'
spawn ssh root@116.62.xxx.xxx
root@116.62.xxx.xxx's password: 
Last login: Tue May  1 09:06:02 2018 from 117.80.xxx.xxx

Welcome to Alibaba Cloud Elastic Compute Service !

[root@xaviyunserver ~]# w;free -m;ls /tmp
 10:42:43 up  2:44,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    117.60.xxx.xxx  08:01    2:01m  0.00s  0.00s -bash
root     pts/1    117.80.xxx.xxx    09:06   13:47   0.00s  0.00s -bash
root     pts/2    117.80.xxx.xxx    10:42    0.00s  0.00s  0.00s w
              total        used        free      shared  buff/cache   available
Mem:           1839          63        1403           0         372        1628
Swap:             0           0           0
12.txt
Aegis-<Guid(5A2C30A2-A87D-490A-9281-6765EDAD7CBA)>
systemd-private-791e0c77625c4133bda4340fcd191439-ntpd.service-mZ3IQT
[root@xaviyunserver ~]# + for ip in '`cat ip.list`'
+ echo 127.0.0.1
127.0.0.1
+ ./exe.expect 127.0.0.1 'w;free -m;ls /tmp'
spawn ssh root@127.0.0.1
root@127.0.0.1's password: 
Last failed login: Tue May  1 10:01:00 CST 2018 from localhost on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Tue May  1 09:06:00 2018 from 192.168.72.1
[root@xavi ~]# w;free -m;ls /tmp
 10:42:43 up  7:26,  3 users,  load average: 0.00, 0.01, 0.05
USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
root     pts/0    192.168.72.1     07:52    1:59m  0.44s  0.44s -bash
root     pts/1    192.168.72.1     09:06    3.00s  2.19s  0.01s /usr/bin/expect ./exe.expect 127.0.0.1 w;
root     pts/3    localhost        10:42    0.00s  0.04s  0.00s w
              total        used        free      shared  buff/cache   available
Mem:           1823         243        1114          17         465        1364
Swap:          3813           0        3813
12.txt
ip.list
list.txt
php-fcgi.sock
systemd-private-59132022d890457896ed0047dcd941fe-cups.service-rfy8W2
systemd-private-59132022d890457896ed0047dcd941fe-httpd.service-h5dZDM
systemd-private-59132022d890457896ed0047dcd941fe-vmtoolsd.service-iZ3m2r
tmp
xavier.sock
相關文章
相關標籤/搜索