天天看點

使用 expect 指令執行自動分發系統

一、指令 except 執行個體詳解

1. 介紹 expect 使用場景

expect可以讓我們實作自動登入遠端機器,并且可以實作自動遠端執行指令。當然若是使用不帶密碼的密鑰驗證同樣可以實作自動登入和自動遠端執行指令。但當不能使用密鑰驗證的時候,我們就沒有辦法了。是以,這時候隻要知道對方機器的賬号和密碼就可以通過expect腳本實作登入和遠端指令。

使用之前先安裝 expect 軟體

yum install -y expect      

2. 自動遠端登入,登陸後不退出

#! /usr/bin/expect
set user "root"
set host "192.168.11.102"
set passwd "123456"
spawn ssh  $user@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"assword:" { send "$passwd\r" }
}
interact      

3. 登入後執行指令,然後退出

#!/usr/bin/expect
set user "root"
set host "192.168.11.18"
set passwd "123456"
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"      

4. 還可以傳遞參數

#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "123456"
set cm [lindex $argv 2]
spawn ssh $user@$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"      

執行:

[root@localhost ~]# ./test.expect root 192.168.11.18 2      

5. 自動同步檔案

#!/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      

6. 指定 host 和要同步的檔案

#!/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@localhost ~]# ./test.expect 192.168.11.18 /tmp/12.txt      

二、建構檔案分發系統

1. 需求背景

對于大公司而言,肯定時不時會有網站或者配置檔案更新,而且使用的機器肯定也是好多台,少則幾台,多則幾十甚至上百台。是以,自動同步檔案是至關重要的。

2. 實作思路

首先要有一台模闆機器,把要分發的檔案準備好,然後隻要使用expect腳本批量把需要同步的檔案分發到目标機器即可。

3. 核心指令

rsync -av --files-from=list.txt  /  root@host:/      

4. 檔案分發系統的實作

 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      

rsync.sh

#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./rsync.expect $ip list.txt
done      

5. 自動批量執行腳本指令

cat 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"      

cat exe.sh

#!/bin/bash
for ip in `cat ip.list`
do
    echo $ip
    ./exe.expect $ip "w;free -m;ls /tmp"
done      

三、擴充 expect 文法介紹

shell腳本需要互動的地方可以使用 Here 文檔是實作,但是有些指令卻需要使用者手動去就互動如passwd、scp。對自動部署免去使用者互動很痛苦,expect能很好的解決這類問題。

1. expect的核心是spawn expect send set

spawn:調用要執行的指令

expect:等待指令提示資訊的出現,也就是捕捉使用者輸入的提示:

send:發送需要互動的值,替代了使用者手動輸入内容

set:設定變量值

interact:執行完成後保持互動狀态,把控制權交給控制台,這個時候就可以手工操作了。如果沒有這一句登入完成後會退出,而不是留在遠端終端上。

expect eof:這個一定要加,與 spawn 對應表示捕獲終端輸出資訊終止,類似于 if....endif

expect 腳本必須以 interact 或 expect eof 結束,執行自動化任務通常 expect eof 就夠了。

2. 設定 timeout

設定 expect 永不逾時

set timeout -1

設定 expect 300 秒逾時,如果超過 300 沒有 expect 内容出現,則推出

set timeout 300