expect經過與rsync結合,能夠在一臺機器上把文件自動同步到多臺機器上linux
[root@linux-5 ~]# cd /usr/local/sbin [root@linux-5 sbin]# vim 4.expect #!/usr/bin/expect set passwd "123456" spawn rsync -av root@192.168.88.10:/root/1.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof
注:expect eof的做用,能夠爲spwan所執行的命令提供足夠的時間執行,不會立刻退出expect,從而致使命令還沒有執行完畢而退出的狀況(尤爲是文件傳輸的命令);interact的做用可使遠程登陸後保留登陸狀態vim
[root@linux-5 sbin]# chmod a+x !$ chmod a+x ./4.expect
[root@linux-5 sbin]# ./4.expect spawn rsync -av root@192.168.88.10:/root/1.txt /tmp/ receiving incremental file list 1.txt sent 43 bytes received 96 bytes 278.00 bytes/sec total size is 6 speedup is 0.04 expect: spawn id exp6 not open while executing "expect eof" (file "./4.expect" line 8)
[root@linux-5 sbin]# cat /tmp/1.txt 12345
經過帶參數的方式爲指定的地址同步指定的文件bash
#!/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@linux-5 sbin]# chmod a+x 5.expect
[root@linux-5 sbin]# ./5.expect 192.168.88.10 /tmp/1.txt spawn rsync -av /tmp/1.txt root@192.168.88.10:/tmp/1.txt sending incremental file list 1.txt sent 96 bytes received 35 bytes 262.00 bytes/sec total size is 6 speedup is 0.05 expect: spawn id exp6 not open while executing "expect eof" (file "./5.expect" line 10)
[root@linux-10 ~]# cat /tmp/1.txt 12345
對於大公司而言,確定時不時會有網站或者配置文件更新,並且使用的機器確定也是好多臺,少則幾臺,多則幾十甚至上百臺。因此,自動同步文件是相當重要的。ssh
首先要有一臺模板機器,把要分發的文件準備好,編寫兩個腳本,一個expect文件分發腳本,一個IP遍歷腳本,將分發腳本嵌套在IP遍歷腳本中,用IP遍歷腳本爲文件分發腳本提供IP參數以及須要同步的文件目錄,再使用expect文件分發腳本批量把須要同步的文件分發到目標機器便可實現。測試
rsync -av --files-from=list.txt / root@host:/網站
使用rsync 的 --files參數,能夠實現調用文件裏面的列表,進行多個文件遠程傳輸,進而實現文件分發ui
#!/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
同步的路徑,須要保證對方機器也有這個相同的路徑,若是沒有路徑,須要使用 -R 選項建立路徑spa
#!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip ./rsync.expect $ip /tmp/file.list done
vim /tmp/file.list ##將須要同步的文件的絕對路徑寫入列表文件中 /usr/local/sbin/1.sh /usr/local/sbin/2.sh /usr/local/sbin/3.sh /root/23.txt
vim /tmp/ip.list 192.168.88.10
[root@linux-5 sbin]# chmod a+x rsync.expect [root@linux-5 sbin]# chmod a+x rsync.sh
[root@linux-5 sbin]# sh -x rsync.sh ++ cat /tmp/ip.list + for ip in '`cat /tmp/ip.list`' + echo 192.168.88.10 192.168.88.10 + ./rsync.expect 192.168.88.10 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / root@192.168.88.10:/ building file list ... done root/ root/23.txt usr/local/ usr/local/sbin/ usr/local/sbin/01.sh usr/local/sbin/02.sh usr/local/sbin/03.sh sent 647 bytes received 101 bytes 1,496.00 bytes/sec total size is 271 speedup is 0.36 expect: spawn id exp6 not open while executing "expect eof" (file "./rsync.expect" line 10)
注:分發系統還有一個重要的關鍵是,確保同步的機器的密碼一致,不然將不能實現同步;因此這就存在一個弊端,一旦腳本暴露,將會讓別人知道如何登錄你機器;固然也有對應的解決辦法,那就是使用密鑰認證,這樣的話,天然在命令行業省去「輸入密碼< password:" { send "$passwd\r" } >''」和「定義密碼< set passwd "123456" >」的命令了.net
與批量分發相似,須要將遠程執行命令腳本嵌套在IP遍歷腳本中,由IP遍歷腳本爲遠程執行命令腳本傳遞IP的參數和所須要執行的具體命令的參數。pwa
vim exe.except #!/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"
vim exe.sh ##利用for循環遍歷IP,傳遞給遠程腳本相應參數,並在循環中執行遠程腳本 #!/bin/bash for ip in `cat /tmp/ip.list` do ./exe.except $ip "w;ls" done
[root@linux-5 sbin]# chmod a+x exe.except [root@linux-5 sbin]# chmod a+x exe.sh
[root@linux-5 sbin]# sh -x exe.sh ++ cat /tmp/ip.list + for ip in '`cat /tmp/ip.list`' + ./exe.except 192.168.88.10 'w;ls' spawn ssh root@192.168.88.10 Last login: Sat Jul 21 16:37:14 2018 from 192.168.88.5 [root@linux-10 ~]# w;ls 16:45:52 up 2:18, 2 users, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT root pts/0 192.168.88.1 15:47 52:08 0.00s 0.00s -bash root pts/1 192.168.88.5 16:45 0.00s 0.00s 0.00s w 1.txt 23.txt anaconda-ks.cfg zabbix-release-3.2-1.el7.noarch.rpm