背景
linux腳本中有很多場景是進行遠程操作的,例如遠程登錄ssh、遠程復制scp、文件傳輸sftp等。這些命令中都會涉及到安全密碼的輸入,正常使用命令時是需要人工手動輸入密碼并接受安全驗證的。為了實現自動化遠程操作,我們可以借用expect的功能。
expect是一個免費的編程工具語言,用來實現自動和交互式任務進行通信,而無需人的干預。expect是不斷發展的,隨著時間的流逝,其功能越來越強大,已經成為系統管理員的的一個強大助手。expect需要Tcl編程語言的支持,要在系統上運行expect必須首先安裝Tcl。
expect的安裝
expect是在Tcl基礎上創建起來的,所以在安裝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 調用要執行的命令
expect腳本必須以interact或expect eof結束,執行自動化任務通常expect eof就夠了。
其他設置
expect編寫語法
expect使用的是tcl語法
cmd arg arg arg
$foo
[cmd arg]
"some stuff"
{some stuff}
示例
login.exp專用于遠程登錄,快捷使用方式: login.exp "exclude" "${remote_ip}" "${remote_user}" "${remote_passwd}" "${remote_command}"
#!/usr/bin/expect -f########################################################### 通過SSH登陸和執行命令#參數: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 360set resssh 0#定義變量標記ssh連接時是否輸入yes確認set inputYes 0set ok_string LOGIN_SUCCESSif {$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
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對武林網的支持。
新聞熱點
疑難解答