背景
linux腳本中有很多場景是進(jìn)行遠(yuǎn)程操作的,例如遠(yuǎn)程登錄ssh、遠(yuǎn)程復(fù)制scp、文件傳輸sftp等。這些命令中都會(huì)涉及到安全密碼的輸入,正常使用命令時(shí)是需要人工手動(dòng)輸入密碼并接受安全驗(yàn)證的。為了實(shí)現(xiàn)自動(dòng)化遠(yuǎn)程操作,我們可以借用expect的功能。
expect是一個(gè)免費(fèi)的編程工具語言,用來實(shí)現(xiàn)自動(dòng)和交互式任務(wù)進(jìn)行通信,而無需人的干預(yù)。expect是不斷發(fā)展的,隨著時(shí)間的流逝,其功能越來越強(qiáng)大,已經(jīng)成為系統(tǒng)管理員的的一個(gè)強(qiáng)大助手。expect需要Tcl編程語言的支持,要在系統(tǒng)上運(yùn)行expect必須首先安裝Tcl。
expect的安裝
expect是在Tcl基礎(chǔ)上創(chuàng)建起來的,所以在安裝expect前我們應(yīng)該先安裝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、安裝完畢以后,進(jìn)入tcl源代碼的根目錄,把子目錄unix下面的tclUnixPort.h copy到子目錄generic中。
2、暫時(shí)不要?jiǎng)h除tcl源代碼,因?yàn)閑xpect的安裝過程還需要用。
(二)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 調(diào)用要執(zhí)行的命令
expect腳本必須以interact或expect eof結(jié)束,執(zhí)行自動(dòng)化任務(wù)通常expect eof就夠了。
其他設(shè)置
expect編寫語法
expect使用的是tcl語法
cmd arg arg arg$foo[cmd arg]"some stuff"{some stuff}示例
login.exp專用于遠(yuǎn)程登錄,快捷使用方式: login.exp "exclude" "${remote_ip}" "${remote_user}" "${remote_passwd}" "${remote_command}"
#!/usr/bin/expect -f########################################################### 通過SSH登陸和執(zhí)行命令#參數(shù):1.Use_Type [check/execute]# 2.SSHServerIp# 3.SSHUser# 4.SSHPassword# 5.CommandList [多個(gè)命令間以;間隔]#返回值:# 0 成功# 1 參數(shù)個(gè)數(shù)不正確# 2 SSH 服務(wù)器服務(wù)沒有打開# 3 SSH 用戶密碼不正確# 4 連接SSH服務(wù)器超時(shí)##########################################################proc usage {} { regsub ".*/" $::argv0 "" name send_user "Usage:/n" send_user " $name Use_Type SSHServerIp SSHUser SSHPassword CommandList/n" exit 1} ## 判斷參數(shù)個(gè)數(shù)if {[llength $argv] != 5} { usage}#設(shè)置變量值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#定義變量標(biāo)記ssh連接時(shí)是否輸入yes確認(rèn)set inputYes 0set ok_string LOGIN_SUCCESSif {$Use_Type=="check"} { #激活ssh連接,如果要需要輸入yes確認(rèn),輸入yes,設(shè)置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確認(rèn),輸入ssh密碼if {$inputYes==1} { expect { -nocase -re "assword: " { send -- "${SSHPassword}/n" set resssh 1 } }}#如果出現(xiàn)try again或者password:提示,說明輸入的用戶密碼錯(cuò)誤,直接退出。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總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)武林網(wǎng)的支持。
新聞熱點(diǎn)
疑難解答
圖片精選