1. 程式人生 > >七十四、expect腳本同步文件、expect腳本指定host和要同步的文件、構建文件分發系統、

七十四、expect腳本同步文件、expect腳本指定host和要同步的文件、構建文件分發系統、

echo 密鑰認證 index bytes 裏的 自動 exp ssh receiving

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

一、expect腳本同步文件

自動同步文件,在一臺機器上同步文件到另一臺機器上去。核心命令是rsync

[root@MRX sbin]# vim 4.expect 路徑:/usr/local/sbin/

#!/usr/bin/expect

set passwd "1346"

spawn rsync -av [email protected]:/tmp/12.txt /tmp/

#把遠程機器上的12.txt同步到這臺機器上來。遠程機器上需要安裝rsync這個命令。從遠程到本機。

expect {

"yes/no" { send "yes\r"}

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

#同理

}

expect eof

#停留在遠程機器上幾秒再退出,不然還沒傳輸就退出來了

[root@MRX sbin]# chmod a+x 4.expect

[root@MRX sbin]# ./4.expect

spawn rsync -av [email protected]:/tmp/12.txt /tmp/

[email protected]'s password:

receiving incremental file list

12.txt


sent 42 bytes received 91 bytes 88.67 bytes/sec

total size is 5 speedup is 0.04


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

指定host和要同步的文件

[root@MRX sbin]# vim 5.expect 路徑:/usr/local/sbin/

#!/usr/bin/expect

set passwd "1346"

set host [lindex $argv 0]

#第一個參數是host

set file [lindex $argv 1]

#第二個參數是file,要同步的文件

spawn rsync -av $file root@$host:$file

#最終形成了一個腳本,從本機到遠程。file一定要寫絕對路徑。

expect {

"yes/no" { send "yes\r"}

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

}

expect eof

[root@MRX sbin]# chmod a+x 5.expect

[root@MRX sbin]# ./5.expect 192.168.93.129 "/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

這種只適合同步一個文件。


三、構建文件分發系統

需求背景

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

實現思路

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

核心命令:

rsync -av --files-from=list.txt / root@host:/ //list.txt:文件列表;files-from,能把文件列表裏的文件都同步上去,文件列表裏的路徑,要用絕對路徑。

[root@MRX sbin]# vim rsync.expect 路徑:/usr/local/sbin/

#!/usr/bin/expect

set passwd "1346"

set host [lindex $argv 0]

set file [lindex $argv 1]

#此時這裏的file代表的就是list這個文件列表,而不是單一的一個文件。

spawn rsync -avR --files-from=$file / root@$host:/ #源目錄是根,目標目錄也是根。-R,不能保證對方機器有沒有這個路徑,就加-R自動創建。

expect {

"yes/no" { send "yes\r"}

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

}

expect eof

還要定義list.txt。文件列表裏的路徑,要保證對方機器上也有這個路徑,文件是要同步的,路徑一定要有。也可以加個-R自動創建。

示例:

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

/tmp/12.txt

/tmp/111.txt

/root/1.txt

/root/123/2.txt

[root@MRX sbin]# vim /tmp/ip.list #因為機器可能不止一臺,所以還要定義一個ip的列表

192.168.93.129

做這個expect腳本的前提是兩臺機器的密碼是一樣的,如果不一樣就只能挨個定義每臺機器的密碼,這樣做又有風險,如果expect腳本泄露,就很危險。解決方法:做密鑰認證,所以expect腳本中password那一步就可以省略,但是yes/no不能省略。

[root@MRX sbin]# vim rsync.sh

#!/bin/bash

for ip in `cat /tmp/ip.list`

do

echo $ip

./rsync.expect $ip /tmp/list.txt

done

[root@MRX sbin]# chmod a+x rsync.expect

[root@MRX sbin]# sh -x rsync.sh

++ cat /tmp/ip.list

+ for ip in '`cat /tmp/ip.list`'

+ echo 192.168.93.129

192.168.93.129

+ ./rsync.expect 192.168.93.129 /tmp/list.txt

spawn rsync -avR --files-from=/tmp/list.txt / [email protected]:/

[email protected]'s password:

building file list ... done

root/

root/1.txt

root/123/

root/123/2.txt

tmp/

tmp/111.txt


sent 400 bytes received 78 bytes 956.00 bytes/sec

total size is 109 speedup is 0.23


四、批量遠程執行命令

這個文件頭一定要寫對,不能寫/bin/bash,不然這些命令無法解析。

[root@MRX sbin]# vim exe.expect 路徑:/usr/local/sbin/

#!/usr/bin/expect

set host [lindex $argv 0]

set passwd "1346"

set cm [lindex $argv 1]

#第二個參數,cm,也就是要執行的命令作為第二個參數

spawn ssh root@$host

expect {

"yes/no" { send "yes\r"}

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

}

expect "]*"

send "$cm\r"

expect "]*"

send "exit\r"

[root@MRX sbin]# chmod a+x exe.expect

[root@MRX sbin]# vim exe.sh

#!/bin/bash

for ip in `cat /tmp/ip.list`

do

echo $ip

./exe.expect $ip "w;free-m;ls /tmp"

done

[root@MRX sbin]# sh exe.sh


七十四、expect腳本同步文件、expect腳本指定host和要同步的文件、構建文件分發系統、