expect腳本同步文件、指定host和要同步的文件、構建文件分發系統、批量遠程執行命令
阿新 • • 發佈:2018-02-27
expect腳本 expect遠程同步 expect腳本同步文件
1.自動同步文件
[root@garytao-01 shell]# vi 4.expect 增加如下腳本內容: #!/usr/bin/expect set passwd "123456" spawn rsync -av [email protected]:/tmp/12.txt /tmp/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof [root@garytao-01 shell]# chmod a+x 4.expect #如果機器上沒有安裝rsync請使用如下命令安裝 [root@garytao-02 ~]# yum -y install rsync
expect腳本指定host和要同步的文件
1.指定host和要同步的文件
[root@garytao-01 shell]# vi 5.expect 增加如下內容: #!/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@garytao-01 shell]# cat 5.expect #!/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@garytao-01 shell]# chmod a+x 5.expect [root@garytao-01 shell]# ./5.expect 172.16.111.110 "/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 28.67 bytes/sec total size is 5 speedup is 0.12 [root@garytao-01 shell]#
expect構建文件分發系統
需求背景
對於大公司而言,肯定時不時會有網站或者配置文件更新,而且使用的機器肯定也是好多臺,少則幾臺,多則幾十甚至上百臺。所以,自動同步文件是至關重要的。
實現思路
首先要有一臺模板機器,把要分發的文件準備好,然後只要使用expect腳本批量把需要同步的文件分發到目標機器即可。
核心命令
rsync -av --files-from=list.txt??/??root@host:/
文件分發系統的實現
## 創建rsync.expect執行腳本 [root@garytao-01 shell]# vi rsync.expect 增加如下腳本內容: #!/usr/bin/expect set passwd "123456" set host [lindex $argv 0] set file [lindex $argv 1] spawn rsync -av --files-from=$file / root@$host:/ expect { "yes/no" { send "yes\r"} "password:" { send "$passwd\r" } } expect eof ## file.list內容,為同步的文件路徑列表 [root@garytao-01 shell]# vi /tmp/file.list 增加如下需要同步的文件路徑: /tmp/12.txt /root/shell/1.sh /root/111/222/lll.txt ## ip.list內容,為需要同步的遠程機器IP列表 [root@garytao-01 shell]# vi /tmp/ip.list 172.16.111.110 127.0.0.1 ##創建一個rsync.sh腳本 [root@garytao-01 shell]# vi rsync.sh #!/bin/bash for ip in `cat /tmp/ip.list` do ./rsync.expect $ip /tmp/file.list done ##加權限執行腳本 [root@garytao-01 shell]# chmod a+x rsync.expect [root@garytao-01 shell]# sh -x rsync.sh ++ cat /tmp/ip.list + for ip in ‘`cat /tmp/ip.list`‘ + ./rsync.expect 172.16.111.110 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / [email protected]:/ [email protected]‘s password: building file list ... rsync: link_stat "/root/shell/1.sh" failed: No such file or directory (2) done root/ root/111/ root/111/222/ root/111/222/lll.txt/ tmp/ sent 130 bytes received 27 bytes 314.00 bytes/sec total size is 5 speedup is 0.03 rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1052) [sender=3.0.9] + for ip in ‘`cat /tmp/ip.list`‘ + ./rsync.expect 127.0.0.1 /tmp/file.list spawn rsync -avR --files-from=/tmp/file.list / [email protected]:/ The authenticity of host ‘127.0.0.1 (127.0.0.1)‘ can‘t be established. ECDSA key fingerprint is 89:19:99:8c:63:ff:d9:e6:19:0d:81:03:27:54:49:78. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added ‘127.0.0.1‘ (ECDSA) to the list of known hosts. [email protected]‘s password: [root@garytao-01 shell]#
備註:如果不能保證對方機器有相同的路徑就加上R,編輯rsync.expect
註意:做分發系統expect腳本的前提是需要保證機器密碼一樣,這樣會有一個問題就是如果密碼泄露的話就會有安全隱患,所以可以做密鑰認證增加安全。
[root@garytao-01 shell]# passwd
更改用戶 root 的密碼 。
新的 密碼:
重新輸入新的 密碼:
passwd:所有的身份驗證令牌已經成功更新。
[root@garytao-01 shell]#
批量遠程執行命令
1.批量執行命令腳本
[root@garytao-01 shell]# 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@garytao-01 shell]# chmod a+x exe.expect
## 定義一個exe的sehll腳本
[root@garytao-01 shell]# vim exe.sh
增加如下腳本內容:
#!/bin/bash
for ip in `cat /tmp/ip.list`
do
./exe.expect $ip "hostname"
done
##執行腳本
[root@garytao-01 shell]# sh exe.sh
spawn ssh [email protected]
[email protected]‘s password:
Last login: Tue Feb 27 11:21:39 2018 from 172.16.111.100
[root@garytao-02 ~]# hostname
garytao-02
[root@garytao-02 ~]# spawn ssh [email protected]
[email protected]‘s password:
Last login: Tue Feb 27 16:52:17 2018 from 172.16.111.1
[root@garytao-01 ~]# hostname
garytao-01
[root@garytao-01 ~]# [root@garytao-01 shell]#
expect腳本同步文件、指定host和要同步的文件、構建文件分發系統、批量遠程執行命令