溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

怎么使用expect命令實現Shell自動化交互

發(fā)布時間:2021-07-01 11:34:21 來源:億速云 閱讀:157 作者:小新 欄目:開發(fā)技術

這篇文章將為大家詳細講解有關怎么使用expect命令實現Shell自動化交互,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

背景

linux腳本中有很多場景是進行遠程操作的,例如遠程登錄ssh、遠程復制scp、文件傳輸sftp等。這些命令中都會涉及到安全密碼的輸入,正常使用命令時是需要人工手動輸入密碼并接受安全驗證的。為了實現自動化遠程操作,我們可以借用expect的功能。

expect是一個免費的編程工具語言,用來實現自動和交互式任務進行通信,而無需人的干預。expect是不斷發(fā)展的,隨著時間的流逝,其功能越來越強大,已經成為系統(tǒng)管理員的的一個強大助手。expect需要Tcl編程語言的支持,要在系統(tǒng)上運行expect必須首先安裝Tcl。

expect的安裝

expect是在Tcl基礎上創(chuàng)建起來的,所以在安裝expect前我們應該先安裝Tcl。

(一)Tcl 安裝

主頁: http://www.tcl.tk

下載地址: http://www.tcl.tk/software/tcltk/downloadnow84.tml

1.下載源碼包

wget http://nchc.dl.sourceforge.net/sourceforge/tcl/tcl8.4.11-src.tar.gz

2.解壓縮源碼包

tar xfvz tcl8.4.11-src.tar.gz

3.安裝配置

cd tcl8.4.11/unix 
./configure --prefix=/usr/tcl --enable-shared 
make 
make install

注意:

1、安裝完畢以后,進入tcl源代碼的根目錄,把子目錄unix下面的tclUnixPort.h copy到子目錄generic中。

2、暫時不要刪除tcl源代碼,因為expect的安裝過程還需要用。

(二)expect 安裝 (需Tcl的庫)

主頁: http://expect.nist.gov/

1.下載源碼包

wget http://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download

2.解壓縮源碼包

tar xzvf expect5.45.tar.gz

3.安裝配置

cd expect5.45 
./configure --prefix=/usr/expect --with-tcl=/usr/tcl/lib --with-tclinclude=../tcl8.4.11/generic 
make 
make install 
ln -s /usr/tcl/bin/expect /usr/expect/bin/expect

expect

expect的核心是spawn、expect、send、set。

spawn 調用要執(zhí)行的命令

  • expect 等待命令提示信息的出現,也就是捕捉用戶輸入的提示:

  • send 發(fā)送需要交互的值,替代了用戶手動輸入內容

  • set 設置變量值

  • interact 執(zhí)行完成后保持交互狀態(tài),把控制權交給控制臺,這個時候就可以手工操作了。如果沒有這一句登錄完成后會退出,而不是留在遠程終端上。

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

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

其他設置

  • 設置expect永不超時 set timeout -1

  • 設置expect 300秒超時,如果超過300沒有expect內容出現,則退出 set timeout 300

expect編寫語法

expect使用的是tcl語法

  • 一條Tcl命令由空格分割的單詞組成. 其中, 第一個單詞是命令名稱, 其余的是命令參數
     cmd arg arg arg

  • $符號代表變量的值. 在本例中, 變量名稱是foo.
     $foo

  • 方括號執(zhí)行了一個嵌套命令. 例如, 如果你想傳遞一個命令的結果作為另外一個命令的參數, 那么你使用這個符號
     [cmd arg]

  • 雙引號把詞組標記為命令的一個參數. "$"符號和方括號在雙引號內仍被解釋
     "some stuff"

  • 大括號也把詞組標記為命令的一個參數. 但是, 其他符號在大括號內不被解釋
     {some stuff}

  • 反斜線符號是用來引用特殊符號. 例如:n 代表換行. 反斜線符號也被用來關閉"$"符號, 引號,方括號和大括號的特殊含義

示例

login.exp專用于遠程登錄,快捷使用方式: login.exp "exclude" "${remote_ip}" "${remote_user}" "${remote_passwd}" "${remote_command}"

#!/usr/bin/expect -f
##########################################################
# 通過SSH登陸和執(zhí)行命令
#參數:1.Use_Type [check/execute]
#  2.SSHServerIp
#  3.SSHUser
#  4.SSHPassword
#  5.CommandList [多個命令間以;間隔]
#返回值:
#  0 成功
#  1 參數個數不正確
#  2 SSH 服務器服務沒有打開
#  3 SSH 用戶密碼不正確
#  4 連接SSH服務器超時
##########################################################
proc usage {} {
 regsub ".*/" $::argv0 "" name
 send_user "Usage:\n"
 send_user " $name Use_Type SSHServerIp SSHUser SSHPassword CommandList\n"
 exit 1
} 
## 判斷參數個數
if {[llength $argv] != 5} {
 usage
}
#設置變量值
set Use_Type [lindex $argv 0]
set SSHServerIp [lindex $argv 1]
set SSHUser [lindex $argv 2]
set SSHPassword [lindex $argv 3]
set CommandList [lindex $argv 4]
#spawn ping ${SSHServerIp} -w 5
#expect {
# -nocase -re "100% packet loss" {
#  send_error "Ping ${SSHServerIp} is unreachable, Please check the IP address.\n"
#  exit 1
# }
#}
set timeout 360
set resssh 0
#定義變量標記ssh連接時是否輸入yes確認
set inputYes 0
set ok_string LOGIN_SUCCESS
if {$Use_Type=="check"} {
 #激活ssh連接,如果要需要輸入yes確認,輸入yes,設置inputYes為1,否則輸入ssh密碼
 spawn ssh ${SSHUser}@${SSHServerIp} "echo $ok_string"
} else {   
 spawn ssh ${SSHUser}@${SSHServerIp} "$CommandList"
}
expect {
 -nocase -re "yes/no" {
  send -- "yes\n"
  set inputYes 1
 }
 -nocase -re "assword: " {
  send -- "${SSHPassword}\n"
  set resssh 1
 }
 #-nocase -re "Last login: " { 
 #  send -- "${CommandList}\n"
 #}
 $ok_string {}
 -nocase -re "Connection refused" {
  send_error "SSH services at ${SSHServerIp} is not active.\n"
  exit 2
 }
 timeout {
  send_error "Connect to SSH server ${SSHUser}@${SSHServerIp} timeout(10s).\n"
  exit 4
 }
}
#如果輸入了yes確認,輸入ssh密碼
if {$inputYes==1} {
 expect {
  -nocase -re "assword: " {
   send -- "${SSHPassword}\n"
   set resssh 1
  }
 }
}
#如果出現try again或者password:提示,說明輸入的用戶密碼錯誤,直接退出。
if {$resssh==1} {
 expect {
  -nocase -re "try again" {
   send_error "SSH user:${SSHUser} passwd error.\n"
   exit 3
  }
  -nocase -re "assword:" {
   send_error "SSH user:${SSHUser} passwd error.\n"
   exit 3
  }
  eof {}
 }
}
send_error -- "$expect_out(buffer)"
#-nocase -re "No such user" {
#  send_error "No such user.\n"
#  exit 5
# }
#exit

關于“怎么使用expect命令實現Shell自動化交互”這篇文章就分享到這里了,希望以上內容可以對大家有一定的幫助,使各位可以學到更多知識,如果覺得文章不錯,請把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI