歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux批量部署工具Expect

Linux批量部署工具Expect

日期:2017/2/28 14:44:39   编辑:Linux教程

前言*隨著IT企業的迅猛發展,Linux運維逐漸趨向於自動化,所謂的自動化運維,常常表現為:從程序打包-代碼管理-上傳-更新部署-代碼回滾等等一系列工作。實現自動化運維後將減去人工的重復工作、避免人工的誤操作等等。

目前主流的自動化工具有puppet、Expect、pssh等等,今天我們來研究一下expect如何來實現自動部署和日常管理維護。

一、Expect簡介
expect是一種能夠按照腳本內容裡面設定的方式與交互式程序進行“會話”的程序。根據腳本內容,Expect可以知道程序會提示或反饋什麼內容以及什麼是正確的應答。它是一種可以提供“分支和嵌套結構”來引導程序流程的解釋型腳本語言。
我們熟知的shell編程功能雖然很強大,但是不能實現有交互功能的多機器之前的操作,例如ssh和scp等。而expect可以幫助我們來實現。

二、Expect安裝
yum install expect -y

三、Expect使用
使用Expect批量管理和部署服務器大致分為兩個步驟,使用for循環讀取服務器IP、密碼列表並取值,遠程執行命令。如下需求,在兩台服務器上執行mkdir /tmp/`date +%Y%m%d`命令,如何實現?
首先定義expect 登陸腳本:
1、login.exp,內容如下(詳細的參數就不解釋了):
#!/usr/bin/expect -f
set ip [lindex $argv 0 ]
set passwd [lindex $argv 1 ]
set command [lindex $argv 2]
set timeout 1
spawn ssh root@$ip
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect "*#*" { send "$command\r" }
expect eof

2、創建批量執行腳本auto_exec.sh
#!/bin/sh
#auto exec command
#wugk 20130712
CMD="$*"
for i in `awk '{print $1}' passwd.txt`
do
j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
expect /data/sh/login.exp $i $j "$CMD"
done

3、建立批量IP、密碼文件
cat passwd.txt內容如下:(第一列為IP,第二列為密碼)
192.168.1.100 abc_123
192.168.1.101 abc_456

四、測試腳本
直接執行:
1 /bin/sh auto_exec.sh "mkdir -p /tmp/`date +%Y%m%d`"

然後登陸兩台服務器查看是否在/tmp/下生產一個以當前系統日期為名稱的目錄。

五、SCP遠程拷貝
如果需要遠程推送文件,重新建立文件login.scp相關參數和auto_exec.sh變量:
1、login.scp內容如下:
#!/usr/bin/expect -f
set ip [lindex $argv 0 ]
set passwd [lindex $argv 1 ]
set src_file [lindex $argv 2]
set des_dir [lindex $argv 3]
set timeout 1
spawn scp -r $src_file root@$ip:$des_dir
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect "#*"
expect eof

2、auto_exec.sh腳本內容如下
123456789101112 #!/bin/sh
#auto exec command
#wugk 20130712
read -p "Please Enter insert Source File or DIR: " src_file
echo ======================================================
sleep 1
read -p "Please Enter insert Destination DIR: " des_dir
for i in `awk '{print $1}' passwd.txt`
do
j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
expect login.scp $i $j $src_file $des_dir
done

密碼文件保持不變即可。

六、一鍵安裝expect、scp批量auto_exec.sh腳本:(其實就是如上幾個腳本合成的)
#!/bin/sh
#auto exec expect shell scripts
#wugk 20130712
if
[ ! -e /usr/bin/expect ];then
yum install expect -y
fi
#Judge passwd.txt exist
if
[ ! -e ./passwd.txt ];then
echo -e "The passwd.txt is not exist......Please touch ./passwd.txt ,Content Example:\n192.168.1.11 passwd1\n192.168.1.12 passwd2"
sleep 2 &&exit 0
fi
#Auto Tuoch login.exp File
cat >login.exp <<EOF
#!/usr/bin/expect -f
set ip [lindex \$argv 0 ]
set passwd [lindex \$argv 1 ]
set src_file [lindex \$argv 2]
set des_dir [lindex \$argv 3]
set timeout 1
spawn scp -r \$src_file root@\$ip:\$des_dir
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "\$passwd\r" }
}
expect "#*"
expect eof
EOF
##Auto exec shell scripts
read -p "Please Enter insert Source File or DIR: " src_file
echo ======================================================
sleep 1
read -p "Please Enter insert Destination DIR: " des_dir
for i in `awk '{print $1}' passwd.txt`
do
j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
expect ./login.exp $i $j $src_file $des_dir

Copyright © Linux教程網 All Rights Reserved