1. 程式人生 > >expect腳本同步文件,expect腳本指定host和要同步的文件,構建文件分發系統,批量遠程執行

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

ron 構建文件分發系統 vim private you 參數 system print idle

expect腳本同步文件
  • 在一臺機器上把文件同步到多臺機器上
  • 自動同步文件
    [root@akuilinux01 sbin]# vim 4.expect 
    #!/usr/bin/expect
    set passwd "s5381561"
    spawn rsync -av [email protected]:/tmp/12.txt /tmp/
    expect {
    "yes/no" { send "yes\r"}
    "password:" { send "$passwd\r" }
    }
    expect eof
    [root@akuilinux01 sbin]# chmod a+x 4.expect 
    [root@akuilinux01 sbin]# ./4.expect 
    spawn rsync -av [email protected]:/tmp/12.txt /tmp/
    [email protected]‘s password: 
    receiving incremental file list
    12.txt
    sent 43 bytes  received 97 bytes  280.00 bytes/sec
    total size is 5  speedup is 0.04
    [root@akuilinux01 sbin]# cat /tmp/12.txt 
    1212
  • expect eof :只有spawn執行的命令結果才會被expect捕捉到,因為spawn會啟動一個進程,只有這個進程的相關信息才會被捕捉到,主要包括:標準輸入的提示信息,eof和timeout。set timeout 定義超時時間(單位為 秒) -1 為永遠不超時

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

  • 指定host和要同步的文件
    [root@akuilinux01 sbin]# vim 5.expect
    #!/usr/bin/expect
    set passwd "s5381561"
    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@akuilinux01 sbin]# chmod a+x 5.expect 
    [root@akuilinux01 sbin]# ./5.expect 192.168.21.129 /root/akui.txt 
    spawn rsync -av /root/akui.txt [email protected]:/root/akui.txt
    [email protected]‘s password: 
    sending incremental file list
    akui.txt
    sent 84 bytes  received 35 bytes  238.00 bytes/sec
    total size is 0  speedup is 0.00
  • 指定目標ip和本機要同步的文件,必須寫絕對路徑

    構建文件分發系統

  • 需求背景:對於大公司而言,肯定時不時會有網站或者配置文件更新,而且使用的機器肯定也是好多臺,少則幾臺,多則幾十甚至上百臺。所以,自動同步文件是至關重要的。
  • 實現思路:首先要有一臺模板機器,把要分發的文件準備好,然後只要使用expect腳本批量把需要同步的文件分發到目標機器即可。
  • 核心命令:rsync -av --files-from=list.txt / root@host:/
  • 使用rsync 的 --files-from參數,可以實現調用文件裏面的列表,進行多個文件遠程傳輸,進而實現文件分發
  • rsync.expect 內容
    [root@akuilinux01 sbin]# vim rsync.expect
    #!/usr/bin/expect
    set passwd "s5381561"
    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可以遞歸創建目錄
  • 創建需要同步IP地址的列表文件
    [root@akuilinux01 sbin]# vim /tmp/ip.list
    192.168.21.129
    127.0.0.1
  • 創建需要同步IP地址的列表文件
    [root@akuilinux01 sbin]# vim /tmp/file.list 
    /root/222/1111/11.txt
    /root/1.txt
  • rsync.sh 內容
    [root@akuilinux01 sbin]# vim rsync.sh
    #!/bin/bash
    for ip in `cat /tmp/ip.list`
    do
     echo $ip
     ./rsync.expect $ip /tmp/file.list
    done
  • 分發系統還有一個重要的關鍵是,確保同步的機器的密碼一致,否則將不能實現同步;所以這就存在一個弊端,一旦腳本暴露,將會讓別人知道如何登陸你機器;當然也有對應的解決辦法,那就是使用密鑰認證,這樣的話,自然在命令行業省去“輸入密碼< password:" { send "$passwd\r" } >‘‘”和“定義密碼< set passwd "123123a" >”的命令了
  • 測試
    root@akuilinux01 sbin]# sh rsync.sh 
    192.168.21.129
    spawn rsync -avR --files-from=/tmp/file.list / [email protected]:/
    [email protected]‘s password: 
    building file list ... done
    root/
    root/1.txt
    root/222/
    root/222/1111/
    root/222/1111/11.txt
    sent 299 bytes  received 63 bytes  724.00 bytes/sec
    total size is 72  speedup is 0.20
    127.0.0.1
    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 SHA256:r2y+hZVvvomXE4d3uRSM0cO+kMdqyWAOMqpTtA2qp6I.
    ECDSA key fingerprint is MD5:ee:da:3b:ea:27:56:38:82:bc:e8:73:10:18:a0:c2:bd.
    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@akuilinux01 sbin]# 

    批量遠程執行命令

  • exe.expect 內容
    [root@akuilinux01 sbin]# vim exe.expect
    #!/usr/bin/expect
    set host [lindex $argv 0]
    set passwd "s5381561"
    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 內容
    [root@akuilinux01 sbin]# vim exe.sh
    #!/bin/bash
    for ip in `cat /tmp/ip.list`
    do
    echo $ip
    ./exe.expect $ip "w;ls /tmp"
    done
  • 測試
    [root@akuilinux01 sbin]# chmod a+x exe.expect 
    [root@akuilinux01 sbin]# sh exe.sh 
    192.168.21.129
    spawn ssh [email protected]
    [email protected]‘s password: 
    Last login: Sun Jul 22 22:19:51 2018 from 192.168.21.128
    [root@akuilinux02 ~]# w;ls /tmp
    23:34:18 up  1:56,  2 users,  load average: 0.08, 0.03, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     22:14   27:06   0.01s  0.01s -bash
    root     pts/1    192.168.21.128   23:34    0.00s  0.01s  0.00s w
    12.txt
    mysql.sock
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-chronyd.service-5DHBEF
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vgauthd.service-sG39NN
    systemd-private-12cfefcc2aaa4137b4ea969d11fbf699-vmtoolsd.service-CVypi4
    [root@akuilinux02 ~]# 127.0.0.1
    spawn ssh [email protected]
    [email protected]‘s password: 
    Last failed login: Sun Jul 22 23:28:56 CST 2018 from 127.0.0.1 on ssh:notty
    There were 2 failed login attempts since the last successful login.
    Last login: Sun Jul 22 21:35:11 2018 from 192.168.21.1
    [root@akuilinux01 ~]# w;ls /tmp
    23:34:18 up  2:05,  2 users,  load average: 0.12, 0.05, 0.05
    USER     TTY      FROM             LOGIN@   IDLE   JCPU   PCPU WHAT
    root     pts/0    192.168.21.1     21:35    2.00s  0.25s  0.00s /usr/bin/expect ./exe.expect 127.
    root     pts/1    127.0.0.1        23:34    0.00s  0.00s  0.00s w
    12.txt
    akuilinux
    aming.sock
    file.list
    ip.list
    lalal
    mysql.sock
    systemd-private-09ebb5bc0a88443db92bc329d77b2efc-chronyd.service-WDveS2
    systemd-private-09ebb5bc0a88443db92bc329d77b2efc-httpd.service-Ok7bke
    systemd-private-09ebb5bc0a88443db92bc329d77b2efc-vgauthd.service-B8RbhF
    systemd-private-09ebb5bc0a88443db92bc329d77b2efc-vmtoolsd.service-WiU5il
    systemd-private-c27889f557f94d1b8cfed596ba761ba5-chronyd.service-kH7zvQ
    systemd-private-c27889f557f94d1b8cfed596ba761ba5-httpd.service-Cj8ABZ
    systemd-private-c27889f557f94d1b8cfed596ba761ba5-vgauthd.service-21n8Br
    systemd-private-c27889f557f94d1b8cfed596ba761ba5-vmtoolsd.service-8VTbyC
    www.sock

    擴展

  • shell多線程

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