1. 程式人生 > 其它 >Expect的安裝與應用,及實現自動檢測另外一臺伺服器執行狀態並重啟,和使用expect指令碼遠端批量管理伺服器與日誌分析

Expect的安裝與應用,及實現自動檢測另外一臺伺服器執行狀態並重啟,和使用expect指令碼遠端批量管理伺服器與日誌分析

技術標籤:安裝配置指令碼# shellExpect的安裝與應用expect遠端批量管理伺服器expect實現自動登入伺服器expect遠端批量日誌分析expect自動檢測及重啟服務

學習Expect

Expect是什麼?

Expect是一個免費的程式設計工具語言,用來實現自動和互動式任務進行通訊,而無需人的干預。
Expect是不斷髮展的,隨著時間的流逝,其功能越來越強大,已經成為系統管理員的的一個強大助手。
Expect需要Tcl程式語言的支援,要在系統上執行Expect必須首先安裝Tcl。

Expect工作原理

從最簡單的層次來說,Expect的工作方式,就像一個通用化的Chat指令碼工具。Chat指令碼最早用於UUCP網路內,以用來實現計算機之間需要建立連線時進行特定的登入會話的自動化。

Chat指令碼由一系列expect-send對組成:expect等待輸出中,輸出特定的字元通常是一個提示符,然後傳送特定的響應。

例如,下面的Chat指令碼實現,等待標準輸出,出現Login:字串,然後傳送somebody作為使用者名稱然後等待 Password:提示符,併發出響應sillyme

安裝Expect

進入 http://www.tcl.tk/software/tcltk/8.5.html
點選 Download Tcl/Tk 8.5.8 Source Releases,選擇下載 tcl8.4.19-src.tar.gz,和tk8.4.19-src.tar.gz;

1. 開始安裝tcl與tk

a). 解壓安裝tcl8.4.19;

$tar zxvf tcl8.4.19-src.tar.gz
$cd tcl8.4.19
$cd unix
$configure
...
updating cache ./config.cache
creating ./config.status
creating Makefile
creating dltest/Makefile
creating tclConfig.sh
$make
$make install

b). 編譯安裝tk8.4.19;

$tar zxvf tk8.4.19-src.tar.gz
$cd tk8.4.19
$cd unix
$configure
...
updating cache ./config.cache
creating ./config.status
creating Makefile
creating dltest/Makefile
creating tclConfig.sh
$make
$make install

解壓安裝expect:

解壓

$tar zxvf expect.tar.gz

安裝

$ ./configure
checking for sin... no
checking for Tcl private headers... checking for tclInt.h... no
configure: error: Can't find Tcl private headers

$ find / -name "tclInt.h"
/home/expect/tcl8.4.19/generic/tclInt.h

$./configure --with-tclinclude=/home/expect/tcl8.4.19/generic/
... ...
checking for Tcl private headers... (cached) found in /home/guoq/osrc/tcl8.4.19/generic
updating cache .././config.cache
creating ./config.status
creating Makefile

$make
$make install

測試指令碼

#!/usr/local/bin/expect -f

set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
set timeout 30

spawn ssh [email protected]$ipaddress
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
interact

實現自動檢測另外一臺伺服器執行狀態,並重啟

haproxy_expect

expect自動檢測並重啟另外一臺伺服器上的程式的程式碼

#!/usr/bin/expect

set ssh_user "fivetrees"
set password "123456"

spawn ssh -i /root/.ssh/$ssh_user Server004.xd.com

expect_before "no)?" {
send "yes\r" }
sleep 0.5

expect "Enter passphrase for key*"
send "$password\r"

expect "*#"
send "/tmp/haproxy.sh\r"

expect "*#"
send "echo\r"

exit

haproxy.sh

#!/bin/bash

Thread=`ps -ef | grep haproxy | grep -v haproxy.sh | grep -v grep`

if [ -z "$Thread" ]
then
        /tmp/haproxy_expect
fi

expect指令碼遠端批量管理伺服器的思路解析

在linux中,使用expect指令碼,實現遠端批量管理伺服器的方法

具體思路:

  1. expect指令碼,通過rsync同步到每臺伺服器,或通過svn來完成,ftp方式也可以。
  2. 執行指令。
  3. 檢查問題。

整個指令碼的構成如下:

  1. 其中,2個配置檔案,一個放IP:iplist,一個放指令config
  2. log目錄下存放的是執行的日誌資訊;
  3. ssh-key目錄下存放的是ssh私鑰檔案,許可權必須為600;
  4. ssh.exp是expect指令碼檔案,需要可執行許可權;
  5. update.sh是主執行程式,需要可執行許可權,通過./update.sh執行,用於從iplist檔案迴圈取值並呼叫expect指令碼。

解析:

  1. 使用迴圈,將IP、埠、帳號、密碼、指令輸出。
  2. 合用expect 進行ssh登入,並執行指令。
  3. 可以使用ssh-key來完成這樣不需要輸入密碼。

使用expect進行日誌分析

因為不同服務的管理方法不同,上次關閉了ssh的外網登入以後,各地不斷有伺服器報出這樣那樣的問題。
主管要求:“全面檢查!” 在檢查中,還真發現不少問題。
最突出的問題是:很多本應該上傳到中心伺服器的日誌,居然一直留在本機沒動彈!時不時發作出來,就撐爆了根分割槽——這當然有分割槽規劃不合理的問題。但線上業務,磁碟劃分修改起來就不是那麼方便了。

於是退而求其次,定期監控日誌檔案大小吧。這回expect只要du -sh一下就行了,方便的很。問題在下一步的分析。

摘舉exp.log中一次迴圈的執行結果:
The authenticity of host '1.2.3.4 (1.2.3.4)' can't be established.
RSA key fingerprint is
bb:d5:81:e1:84:09:c5:32:f6:fb:e1:b3:d3:de:c3:53.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '1.2.3.4' (RSA) to the list of known hosts.
[email protected]'s password:
4.0K /home/apache2/logs/access_log

#!/bin/bash
nk=`sed -n -e "/50M/=" exp.log`
nnk=`expr $nk - 1`
sed -n "$nnk"p"" exp.log|awk -F"'" '{print $1}'|awk -F"@" '{print $2}'

但問題是:如果同時有兩臺到50M呢?或者在執行到它時,已經到50M以上呢? 於是我想,以ls -sh顯示大小,人眼好看,電腦不好認啊。如果用du -b,那大小相同的機率就應該小很多很多了。然後定一個閥值,進行比較迴圈就可以了。

#!/bin/bash
for ip in `cat ip.lst`
do
./ssh.exp $ip > /dev/null 2&>1
done
bs=400
size=`grep access exp.log | awk '{if ($1>'"$bs"'){print $1}}'`
for so in $size
do
nk=`sed -n -e "/$so/=" exp.log`
nnk=`expr $nk - 1`
sed -n "$nnk"p"" exp.log | awk -F"'" '{print $1}'|awk [email protected] '{print $2}'
done

本來想用sed ‘N;s/n//g’ exp.log來合併行尾,省得調行號,但exp日誌的格式因為ssh登入的提示資訊不一而無法統一,只能放棄。
試驗性的在ip.lst中輸入了15個IP,執行結果顯示出來了兩個,成功。