1.什么是expect
????expect是基于TCL的相對簡單的一個免費的腳本編程工具語言,用來實現自動和交互式任務程序語言進行通信,無需人工干預,其作者定義為:expect is a software suite for automating interactive tools
2.expect程序的工作流程
????except的工作流程可以理解為: spawn啟動進程 --> expect期待關鍵字 --> send向進程發送字符 --> 退出結束
3.安裝except
yum install expect -y
4.一個小實例
#!/usr/bin/expect
spawn ssh -p22 root@192.168.1.25 /usr/sbin/ifconfig -a
set timeout 60
expect "*password:" {send "root1234\n"}
expect eof
exit
5.expect語法
????expect命令的語法:
命令 [選項] 參數
5.1 spwan
????spwan命令是expect的初始命令,它啟動一個進程,之后所有expect操作都在這個進程中進行,其使用方法如下:
spwan ssh root@192.168.1.26
????在spwan命令后面,直接加上要啟動的進程、命令等信息,除此之外,還支持其他選項:
-open 啟動文件進程,具體說明請自行查閱
-ignore 忽略某些信號,具體說明請自行查閱
5.2 expect
????使用方法:
expect 表達式 動作 表達式 動作 ... ...
????expect命令用于等候一個相匹配內容的輸出,一旦匹配則執行expect后面的動作命令,這個命令接收幾個特有參數,用的最多的就是-re,表示使用正則表達式進行匹配,如下:
spwan ssh root@192.168.1.26
expect -re ".*password" {send "root1234\r"}
????expect是依附與spwan命令的,當執行ssh命令后,expect就會匹配命令執行后的輸出,然后執行expect后面包含在{}中的send或exp_send動作,匹配及動作可以放在下一行,就可以省略{},如下:
spwan ssh root@192.168.1.26
expect -re ".*password"
send "root1234\r"
5.3 exp_send和send
????在上面的介紹中,我們看到了exp_send命令的使用,exp_send是expect中的動作,send和其作用一樣。exp_send命令可以發送一些特殊符號:
\r 回車
\n 換行
\t 制表符
????send命令還有幾個可選的參數:
-i 指定spwan_id, 這個參數用來向不同的spwan_id的進程發送命令,是進行多進程控制的關鍵參數。
-s s代表slowly,也就是控制發送的速度,這個參數使用的時候要與expect中的變量send_slow向關聯
5.4 exp_continue
????這個命令一般用在動作中,它被使用的條件比較苛刻,例子:
#!/usr/bin/expect
spwan ssh -p22 root@192.168.1.26 /usr/sbin/ifconfig eth0
set timeout 60
expect {
-timeout 1
"yes/no" { exp_send "yes\r"; exp_continue }
"*password:" { exp_send "root1234\r" }
timeout { puts "expect was timeout"; return }
}
expect eof
exit
????個人理解:spwan后面的命令的輸出,從第一行開始進行expect遍歷匹配,若匹配則執行動作,不匹配則進行下條匹配,而exp_continue的作用是,若不匹配則跳過此行輸出,并進行第二行輸出進行expect的匹配
5.5 send_user
????send_user用于把后面的參數輸出到標準輸出中去,默認的send、exp_send都是將參數輸出到程序中去,用法:
send_user "please input password:"
????這樣就可以在標準輸出中打印please input password:字符了。
#!/usr/bin/expect
if { $argc != 3 } {
send_user "usage: expect scp-expect.exp file host dir\n"
exit
}
5.6 exit
????exit命令功能很簡單,就是直接退出腳本,但可以利用這個命令做一些掃尾工作,如:
exit -onexit {
exec rm $tmpfile
send_user "Good Bye\n"
}
6. expect變量
????expect中有很多有用的變量,他們的使用方法與TCL中的變量相同,如:
set 變量名 變量值 # 設置變量的方法
puts $變量名 # 讀取變量的方法
# define var
set file [lindex $argv 0] # 取命令行參數的
set host [lindex $argv 1]
set dir [lindex $argv 2]
set password "root1234"
7. expcct關鍵字
????expect中的關鍵字用于匹配過程,代表某些特殊含義或狀態,一般用于expect命令中而不能在外面使用,也可以理解為事件,如:
expect eof {}
7.1 eof
????eof(end-of-file)關鍵字用于匹配結束符,比如文件、ftp傳輸停止等,在這個關鍵字后跟上動作來做進一步的控制,特別是ftp交互操作方面,他的作用很大,如:
spawn ftp root@192.168.1.26
expect {
"password:" {exp_send "root1234"}
eof { ftp connect close }
}
interact { }
????interact也是關鍵字,可以是當前停留在服務端,交互模式使用
7.2 timeout
????timeout是expect中的一個重要變量,它是一個全局性的時間控制開關,你可以通過為這個變量賦值來規定整個expect操作的時間,注意這個變量是服務與expect全局的。例子:
set timeout 60
spawn ssh root@192.168.1.26
expect "*password:" { send "root1234\n" }
expect timeout {puts "timeout"; return }
????先將超時間設置為60秒,當程序阻塞到60秒,就會激活timeout動作。
????另一種設置timeout的方法,如下:
spwan ssh root@192.168.1.26
expect {
-timeout 60
-re "password:" {exp_send "root1234\r"}
-re "TopsecOS#" { }
timeout { puts "timeout"; return }
}