一、expect同步文件: 自動同步文件; 一臺機器向一臺機器同步文件:shell
1:在一臺機器上,把文件同步到其餘機器上;並添加執行權限:vim
[root@localhost_01 sbin]# vim 4.expect [root@localhost_01 sbin]# cat 4.expect #!/usr/bin/expect set passwd "nihao123!" spawn rsync -av -e "ssh -p 56888" root@192.168.149.129:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send $passwd\r} } expect eof [root@localhost_01 sbin]# chmod a+x 4.expect
2:執行這個腳本: ./4.expect 並查看同步後的文件內容:安全
[root@localhost_01 sbin]# ./4.expect spawn rsync -av -e ssh -p 56888 root@192.168.149.129:/tmp/12.txt /tmp/ root@192.168.149.129's password: receiving incremental file list 12.txt sent 43 bytes received 96 bytes 278.00 bytes/sec total size is 5 speedup is 0.04 [root@localhost_01 sbin]# ls /tmp/12.txt /tmp/12.txt [root@localhost_01 sbin]# ll /tmp/12.txt -rw-r--r-- 1 root root 5 Oct 4 06:04 /tmp/12.txt
註釋:expect eof表示只有spawn執行命令的結果纔會被expect捕捉到,由於spawn會啓動一個進程,只有這個進程相關信息纔會被捕捉到,注意包括標準輸入的提示信息,eof 和timeout:bash
以下測試,註釋掉expect eof,則還沒來得及傳輸了,就退出來了(僅僅到輸入密碼的那裏)ssh
[root@localhost_01 sbin]# vim 4.expect #!/usr/bin/expect set passwd "nihao123!" spawn rsync -av -e "ssh -p 56888" root@192.168.149.129:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send $passwd\r} } #expect eof #註釋掉: [root@localhost_01 sbin]# rm -fr /tmp/12.txt [root@localhost_01 sbin]# ./4.expect spawn rsync -av -e ssh -p 56888 root@192.168.149.129:/tmp/12.txt /tmp/ root@192.168.149.129's password: [root@localhost_01 sbin]#
二、expect指定host和要同步的文件:測試
[root@localhost_01 sbin]# vim 5.expect [root@localhost_01 sbin]# cat 5.expect #!/usr/bin/expect set passwd "nihao123!" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av -e "ssh -p 56888" $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r"} } expect eof [root@localhost_01 sbin]# chmod a+x 5.expect
註釋:用變量定義的文件所在目錄要寫絕對路徑纔可:網站
如:同步本機目錄/tmp/12.txt到遠端主機192.168.149.129的/tmp/目錄下:ui
[root@localhost_01 sbin]# ./5.expect 192.168.149.129 "/tmp/12.txt" spawn rsync -av -e ssh -p 56888 /tmp/12.txt root@192.168.149.129:/tmp/12.txt root@192.168.149.129's password: sending incremental file list 12.txt sent 100 bytes received 35 bytes 90.00 bytes/sec total size is 9 speedup is 0.07
註釋:由於同步rsync須要藉助ssh的端口,若是是默認的22端口或者自定義的,那若是有多臺機器的ssh的端口各不相同,那是否能夠把port當成一個參數來傳遞:以下:spa
[root@localhost_01 sbin]# vim 5.expect #!/usr/bin/expect set passwd "nihao123!" set port [lindex $argv 0] set host [lindex $argv 1] set file [lindex $argv 2] spawn rsync -av -e "ssh -p $port" $file root@$host:$file expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r"} } expect eof [root@localhost_01 sbin]# ./5.expect 56888 192.168.149.129 "/tmp/12.txt" spawn rsync -av -e ssh -p 56888 /tmp/12.txt root@192.168.149.129:/tmp/12.txt root@192.168.149.129's password: sending incremental file list sent 44 bytes received 12 bytes 37.33 bytes/sec total size is 9 speedup is 0.16
二、構建文件分發系統;.net
一臺機器向多臺機器同步文件:
需求背景:對於大公司而言,確定會時不時有網站或者配置文件更新,並且使用的機器也是好多臺,少則幾臺,多則上百臺,因此自動同步文件是很重要的:
實現思路:首先須要有一臺模板機器,提早把要分發的文件準備好,而後只要使用expect腳本批量把須要同步的文件分發到目標機器便可:
核心命令:rsync -av --filep-from=list.txt / root@host:/
註釋:使用rsync 的 --files參數,能夠實現調用文件裏面的列表(列表裏的路徑須要絕對路徑),進行多個文件遠程傳輸,進而實現文件分發:
1:建立rsync.expect的腳本內容:
[root@localhost_01 sbin]# vim rsync.expect [root@localhost_01 sbin]# cat rsync.expect #!/usr/bin/expect set passwd "nihao123!" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -avR -e "ssh -p 56888" --files-from=$file / root@$host:/ #這個地方定義了原目錄和目標目錄以跟目錄開始 expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof [root@localhost_01 sbin]# chmod a+x rsync.expect
註釋:文件路徑放在/tmp/flie.list:同步的路徑,咱們要把要同步的文件路徑放在/tmp/list.txt裏面,這個時候須要保證對方機器上也要有個這個路徑(文件有沒有無所謂),若是沒有路徑,則須要使用rsync -avR建立路徑:
註釋:IP列表 /tmp/ip.list:實現分發系統,遠程同步的機器數量大,還須要定義一個IP列表 /tmp/ip.txt;
建立同步文件的列表和建立分發系統的IP地址列表:
註釋:同步前提是分發系統的密碼須要都同樣才能夠,可是這也容易形成系統不安全(expect腳本泄露),不過能夠也使用祕鑰驗證(rsync驗證),省略掉密碼的哪一步;
2:建立同步文件列表(寫絕對路徑):/tmp/file.list
[root@localhost_01 sbin]# vim /tmp/flie.list /tmp/12.txt /root/shell/test.sh /root/111/222/111.txt
3:建立IP地址列表: /tmp/ip.list
[root@localhost_01 sbin]# vim /tmp/ip.list 192.168.149.129 192.168.149.131
4:建立rsync.sh腳本: 並給予執行權限:
[root@localhost_01 sbin]# vim rsync.sh #!/bin/bash for ip in `cat /tmp/ip.list` do echo $ip ./rsync.expect $ip /tmp/file.list done [root@localhost_01 sbin]# chmod a+x rsync.sh
5:執行rsync.sh腳本: ./rsync.sh
[root@localhost_01 sbin]# sh rsync.sh 192.168.149.129 spawn rsync -avR -e ssh -p 56888 --files-from=/tmp/file.list / root@192.168.149.129:/ root@192.168.149.129's password: building file list ... done root/ root/111/ root/111/222/ root/111/222/111.txt root/shell/ root/shell/test.sh tmp/ sent 415 bytes received 69 bytes 322.67 bytes/sec total size is 118 speedup is 0.24 192.168.149.131 spawn rsync -avR -e ssh -p 56888 --files-from=/tmp/file.list / root@192.168.149.131:/ root@192.168.149.131's password: building file list ... done root/ root/111/ root/111/222/ root/111/222/111.txt root/shell/ root/shell/test.sh tmp/ tmp/12.txt sent 467 bytes received 88 bytes 1,110.00 bytes/sec total size is 118 speedup is 0.21
註釋:這個shell腳本的目的,就是遍歷一下IP列表中的IP地址;
註釋:重要的是expect腳本要加上執行權限,而且兩端都要安裝rsync服務:
註釋:分發系統還有一個重要的關鍵是,確保同步的機器的密碼以及端口號(由於rsync要以來sshd的端口)一致,不然將不能實現同步;因此這就存在一個弊端,一旦腳本暴露,將會讓別人知道如何登錄你機器;固然也有對應的解決辦法,那就是使用密鑰認證,這樣的話,天然在命令行業省去「輸入密碼< password:" { send "$passwd\r" } >''」和「定義密碼< set passwd "123123a" >」的命令了;
二、批量遠程執行命令; 有時候遠端機器操做完後,須要遠程重啓,或者重啓服務等,則使用這個方法;
1: 建立exe.expcet腳本: 並給執行權限;
[root@localhost_01 sbin]# vim exe.expect #!/usr/bin/expect set port [lindex $argv 0] set host [lindex $argv 1] set cm [lindex $argv 2] set passwd "nihao123!" spawn ssh $port root@$host expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect "]*" send "$cm\r" expect "]*" send "exit\r" [root@localhost_01 sbin]# chmod a+x exe.expect
2:建立exe.sh執行腳本:
#!/bin/bash for ip in `cat ip.list` do echo $ip ./exe.expect $port -p56888 $ip "hostname" done
3:執行腳本: sh exe.sh
[root@localhost_01 sbin]# sh exe.sh 192.168.149.129 spawn ssh -p56888 root@192.168.149.129 root@192.168.149.129's password: Last login: Thu Oct 4 07:12:29 2018 from 192.168.149.130 [root@localhost_02 ~]# hostname localhost_02 [root@localhost_02 ~]# 192.168.149.131 spawn ssh -p56888 root@192.168.149.131 root@192.168.149.131's password: Last login: Thu Oct 4 20:19:06 2018 from 192.168.149.135 [root@localhost_03 ~]# hostname localhost_03