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

expect腳本同步文件

  • 在一臺機器上把文件同步到多臺機器上
  • 自動同步文件 vim 4.expect 
[root@yong-01 sbin]# vim 4.expect

#!/usr/bin/expect
set passwd "20655739"
spawn rsync -av root@192.168.181.135:/tmp/12.txt /tmp/
expect {
"yes/no" { send "yes\r"}
"password:" { send $passwd\r}
}
expect eof


查看同步過程
[root@yong-01 sbin]# chmod a+x 4.expect 
[root@yong-01 sbin]# ./4.expect 
spawn rsync -av root@192.168.181.135:/tmp/12.txt /tmp/
The authenticity of host '192.168.181.135 (192.168.181.135)' can't be established.
ECDSA key fingerprint is SHA256:oYqovve1b2BwHDBYcFasCiiFzZTHJvKDbTGZAjmlMXc.
ECDSA key fingerprint is MD5:3d:f8:af:0d:85:48:db:2a:46:0e:68:5f:eb:43:3e:43.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.181.135' (ECDSA) to the list of known hosts.
receiving incremental file list
12.txt

sent 30 bytes  received 84 bytes  228.00 bytes/sec
total size is 5  speedup is 0.04
[root@yong-01 sbin]# ls /tmp/12.txt 
/tmp/12.txt
[root@yong-01 sbin]# ll /tmp/12.txt 
-rw-r--r-- 1 root root 5 7月  21 00:48 /tmp/12.txt
  • expect eof :只有spawn執行的命令結果纔會被expect捕捉到,由於spawn會啓動一個進程,只有這個進程的相關信息纔會被捕捉到,主要包括:標準輸入的提示信息,eof和timeout。

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

  • set timeout 定義超時時間(單位爲 秒) -1 爲永遠不超時
  • 指定host和要同步的文件 vim 5.expect
[root@yong-01 sbin]# vim 5.expect 

#!/usr/bin/expect
set passwd "20655739"
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
  • 變量定義的文件地址,使用時,必須寫絕對路徑
  • 本機文件同步到遠程機器上去: ./5.expect 192.168.181.135 "/tmp/12.txt" 
  • [root@yong-01 sbin]# ./5.expect 192.168.181.135 "/tmp/12.txt"
    spawn rsync -av /tmp/12.txt root@192.168.181.135:/tmp/12.txt
    sending incremental file list
    12.txt
    
    sent 79 bytes  received 31 bytes  220.00 bytes/sec
    total size is 5  speedup is 0.05
    expect: spawn id exp6 not open
        while executing
    "expect eof"
        (file "./5.expect" line 10)

     

構建文件分發系統

  • 需求背景
    • 對於大公司而言,確定時不時會有網站或者配置文件更新,並且使用的機器確定也是好多臺,少則幾臺,多則幾十甚至上百臺。因此,自動同步文件是相當重要的。
  • 實現思路
    • 首先要有一臺模板機器,把要分發的文件準備好,而後只要使用expect腳本批量把須要同步的文件分發到目標機器便可。
  • 核心命令
    • rsync -av --files-from=list.txt / root@host:/
    • 使用rsync 的 --files參數,能夠實現調用文件裏面的列表,進行多個文件遠程傳輸,進而實現文件分發
    • 文件分發系統的實現

一、rsync.expect 內容shell

[root@yong-01 sbin]# vim rsync.expect

#!/usr/bin/expect
set passwd "20655739"
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 建立路徑
  • 由於實現分發系統,確定是由於須要分發的機器數量過大,因此,定義好了 文件 的 list 列表文件之後, 還須要配置 ip 的列表文件
  • 建立須要同步文件的列表文件
  • 建立須要同步IP地址的列表文件
  1. rsync.sh 內容
#!/bin/bash 
for ip in `cat /tmp/ip.list`
do
    echo $ip   
    ./rsync.expect $ip /tmp/file.list
done

執行過程
[root@yong-01 sbin]# sh -x rsync.sh
++ cat /tmp/ip.list
+ for ip in '`cat /tmp/ip.list`'
+ echo 192.168.181.135
192.168.181.135
+ ./rsync.expect 192.168.181.135 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.181.135:/
building file list ... done
root/

sent 114 bytes  received 15 bytes  258.00 bytes/sec
total size is 1051  speedup is 8.15
expect: spawn id exp6 not open
    while executing
"expect eof"
    (file "./rsync.expect" line 10)
+ for ip in '`cat /tmp/ip.list`'
+ echo 192.168.181.138
192.168.181.138
+ ./rsync.expect 192.168.181.138 /tmp/file.list
spawn rsync -avR --files-from=/tmp/file.list / root@192.168.181.138:/
root@192.168.181.138's password: 
building file list ... done
root/
root/shell/
root/shell/case.sh
tmp/
tmp/12.txt

sent 1257 bytes  received 59 bytes  2632.00 bytes/sec
total size is 1051  speedup is 0.80
  • 這個sh 的目的,就是遍歷一下 ip列表文件中的 ip地址vim

  • 最重要的,expect腳本 必須加入執行權限bash

  • 文件不存在,會報錯ssh

  • 分發系統還有一個重要的關鍵是,確保同步的機器的密碼一致,不然將不能實現同步;因此這就存在一個弊端,一旦腳本暴露,將會讓別人知道如何登錄你機器;固然也有對應的解決辦法,那就是使用密鑰認證,這樣的話,天然在命令行業省去「輸入密碼< password:" { send "$passwd\r" } >''」和「定義密碼< set passwd "123123a" >」的命令了網站

批量遠程執行命令

  • exe.expect 內容
#!/usr/bin/expect
set host [lindex $argv 0]
set passwd "20655739"
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"
  • exe.sh 內容
#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done
相關文章
相關標籤/搜索