1、安裝步驟
①首先,准備好所需的兩個安裝包
tcl8.4.13-src.tar.gz(鏈接:https://pan.baidu.com/s/1yTFtr1zZbYkBnIenm-HWGg ,提取碼:h8ed )
expect-5.43.0.tar.gz(鏈接:https://pan.baidu.com/s/1ZdJo-nRI_FnUYiMzkKqkhA ,提取碼:90g5 )
②安裝tcl8.4.13
Tcl 的 configure 腳本有一個語法錯誤,下面的命令可以糾正它:
sed -i "s/relid'/relid/" configure (8.4.13不再有這樣的錯誤了)
編譯tcl
cd unix ./configure --prefix=/expect make make install mkdir -p /tools/lib
需要的內容都拷貝到/tools/lib目錄
cp tclConfig.sh /tools/lib/
安裝完畢完先不要刪除源碼,以會安裝expect還要用到
將/tools/bin目錄export到環境變量,
③安裝expect
./configure --prefix=/tools --with-tcl=/tools/lib --with-x=no
如果報
checking for Tcl private headers... checking for tclInt.h... no
configure: error: Can't find Tcl private headers
就再添加一個頭文件目錄參數
--with-tclinclude=../tcl8.4.13/generic,即:
./configure --prefix=/tools --with-tcl=/tools/lib --with-x=no --with-tclinclude=../tcl8.4.13/generic
腳本運行正常,進行執行make進行編譯
make
編譯過程中未出現錯誤,執行安裝:
make install
編譯完成后會生在/tools/bin內生成expect命令
. .bash_profile
此時在命令行執行expect應該可以執行了!
④完成安裝
下面就可以做expect的工作了!
以下轉自:https://mp.weixin.qq.com/s/oM6084HOzpS6mp1RQQoUpg
2、 expect的常用命令
命令 |
說明 |
spawn |
啟動新的交互進程, 后面跟命令或者指定程序 |
expect |
從進程中接收信息, 如果匹配成功, 就執行expect后的動作 |
send |
向進程發送字符串 |
send exp_send |
用於發送指定的字符串信息 |
exp_continue |
在expect中多次匹配就需要用到 |
send_user |
用來打印輸出 相當於shell中的echo |
interact |
允許用戶交互 |
exit |
退出expect腳本 |
eof |
expect執行結束, 退出 |
set |
定義變量 |
puts |
輸出變量 |
set timeout |
設置超時時間 |
3、 作用原理簡介
3.1 示例腳本
這里以ssh遠程登錄某台服務器的腳本為例進行說明, 假設此腳本名稱為remote_login.sh:
#!/usr/bin/expect set timeout 30 spawn ssh -l root 172.16.22.131 expect "password*" send "123456\r" interact
3.2 腳本功能解讀
(1) #!/usr/bin/expect
上述內容必須位於腳本文件的第一行, 用來告訴操作系統, 此腳本需要使用系統的哪個腳本解析引擎來執行.具體路徑可通過command -v expect命令查看.
注意:這里的expect和Linux的bash、Windows的cmd等程序一樣, 都是一種腳本執行引擎.腳本需要有可執行權限(chmod +x remote_login.sh, 或chmod 755 auto_login.sh), 然后通過命令./remote_login.sh運行即可;如果輸入sh remote_login.sh, 意義就不一樣了: 明確調用sh引擎去執行此腳本, 此時首行的#!/usr/bin/expect就失效了.
(2) set timeout 30
設置連接的超時時間為30秒.(3) spawn ssh -l root 172.16.22.131spawn、send等命令是expect工具中的內部命令, 如果沒有安裝expect工具, 就會出現"spawn not found"等錯誤.
不要用which spawn之類的命令去找spawn, 因為並沒有這樣的程序.
(4) expect "password*"
這個命令用來判斷上次輸出結果里是否包含"password*"的字符串, 如果有則立即返回, 否則就等待一段時間后返回. 這里的等待時長就是前面設置的timeout, 也就是30秒.
(5) send "123456\r"
這里就是執行交互動作, 作用等同於手工輸入密碼.提示: 命令字符串結尾加上\r, 這樣的話, 如果出現異常等待的狀態就能夠停留下來, 作進一步的核查.
(6) interact
expect執行完成后保持用戶的交互狀態, 這個時候用戶就可以手工操作了.如果沒有這一句, expect執行完成后就會退出腳本剛剛遠程登錄過去的終端, 用戶也就不能繼續操作了.
4 其他腳本使用示例
4.1 直接通過expect執行多條命令
注意首行內容, 這種情況下就只能通過./script.sh來執行這類腳本了:
#!/usr/bin/expect -f set timeout 10 # 切換到root用戶, 然后執行ls和df命令: spawn su - root expect "Password*" send "123456\r" expect "]*" # 通配符 send "ls\r" expect "#*" # 通配符的另一種形式 send "df -Th\r" send "exit\r" # 退出spawn開啟的進程 expect eof # 退出此expect交互程序
4.2 通過shell調用expect執行多條命令
注意首行內容, 這種情況下可通過sh script.sh、bash script.sh 或./script.sh, 都可以執行這類腳本:
#!/bin/bash ip="172.16.22.131" username="root" password="123456" # 指定執行引擎 /usr/bin/expect <<EOF set time 30 spawn ssh $username@$ip df -Th expect { "*yes/no" { send "yes\r"; exp_continue } "*password:" { send "$password\r" } } expect eof EOF
5 spawn not found 的解決
出現這個錯誤的基本上都是出學者: Linux 執行shell腳本有兩種方式:
一種是將腳本作為sh的命令行參數, 如sh remote_login.sh, 或sh /data/remote_login.sh;一種是將腳本作為具有執行權限的可執行腳本, 如./remote_login.sh, 或/data/remote_login.sh.
而作為sh命令行參數來運行, 那么腳本第一行的#!/usr/bin/expect就會失效, 所以才會出現spawn not found、send not found等錯誤, 所有上面的automate_login.sh腳本必須用以下命令運行:
./automate_expect.sh