- expect介紹
expect時用與提供自動互動的工具(工作需要telnet/ssh自動登入遠端裝置并進行一些固定操作)。比如如果想要用ssh登陸伺服器,每次都輸入密碼你覺得麻煩,那你就可以使用expect來做自動互動,這樣的話就不用每次都輸入密碼了。
先看例子:
#!/usr/bin/expect #set timeout 20 #設定逾時時間 spawn ssh [email protected] expect "*password:" send "123\r"# expect "*#" interact |
解釋:
1.#!/usr/bin/expect :需要先安裝軟體,然後來說明用expect來執行
2.spawn ssh [email protected] :spawn是進入expect環境後才可以執行的expect内部指令,用來執行它後面的指令。
3.expect "*password:" :也是expect的内部指令,用來解惑關鍵的字元串,如果有,就會立即傳回下面設定的内容,如果沒有就看是否設定了逾時時間。
4.send "123\r":這時執行互動式動作,與手工輸入密碼等效,在expect截獲關鍵字之後,它就會輸入send後面的内容。
5.interact :執行完畢後把持互動狀态,把控制台,這時候就可以進行你想要進行的操作了。如果沒有這一句,在登陸完成之後就會退出,而不是留在遠端終端上。
2.源碼下載下傳
expect5.45.3.tar.gz:
https://sourceforge.net/projects/expect/files/Expect/5.45/expect5.45.tar.gz/download
tcl8.6.8-src.tar.gz:
http://tcl.sourceforge.net/
也可以從CSDN下載下傳。
建立目錄soft,将下載下傳的expect5.45.3.tar.gz、tcl8.6.8-src.tar.gz放在soft目錄下。
3.交叉編譯TCL
由于expect是基于tcl語言的,是以移植expect前,需先交叉編譯tcl。
[email protected]:/home/hanyang/soft# tar zxf tcl8.6.8-src.tar.gz
[email protected]:/home/hanyang/soft# mkdir install_tcl
[email protected]:/home/hanyang/soft# cd tcl8.6.8/unix/
注意,/configure …… 之前應該先:
[email protected]:export tcl_cv_strtod_buggy=1
[email protected]:export ac_cv_func_strtod=yes
否則,交叉編譯tcl時會出現以下錯誤(普通編譯無此錯誤)
fixstrtod.o: In function `fixstrtod':
fixstrtod.c:(.text+0x0): multiple definition of `fixstrtod'
strtod.o:strtod.c:(.text+0x0): first defined here
[email protected]:/home/hanyang/soft/tcl8.6.8/unix#export CC=/home/hanyang/BRD_BSP_Version/export/brd_bsp/tools/ppc_gcc4.8.2_glibc2.18.0_multi/bin/ppce6500-hardfloat-linux-gnu-gcc
[email protected]:/home/hanyang/soft/tcl8.6.8/unix#
./configure --prefix=/home/hanyang/soft/install_tcl/ --host=powerpc
生成Makefile
(configure時留意一下是否有這句話,沒有的話可能有問題checking for powerpc-gcc... /home/hanyang/BRD_BSP_Version/export/brd_bsp/tools/ppc_gcc4.8.2_glibc2.18.0_multi/bin/ppce6500-hardfloat-linux-gnu-gcc
)
[email protected]:/home/hanyang/soft/tcl8.6.8/unix# make
[email protected]:/home/hanyang/soft/tcl8.6.8/unix# make install
在/home/hanyang/soft/install_tcl/lib路徑下擷取libtcl8.6.so和tcl8.6檔案夾。
(file libtcl8.6.so 看一下是否是交叉編譯運作在PowerPC下的 。)
4.編譯expect
[email protected]:/home/hanyang/soft# tar zxf expect5.45.3.tar.gz
注意,expect不支援交叉編譯,是以在configure,需要先用預設的X86 gcc編譯,等configure後再修改Makefile
[email protected]:/home/hanyang/soft/expect5.45.3# ./configure \
--prefix=/home/hanyang/soft/install_expect/ \
--with-tcl=/home/hanyang/soft/install_tcl/lib \
--with-x=no \
--with-tclinclude=/home/hanyang/soft/install_tcl/include \
1)--prefix=/home/hanyang/soft/install_expect/:指定安裝目錄
2)--with-tcl=/home/hanyang/soft/install_tcl/lib:指定tcl所在目錄,通常指定為tcl安裝目錄下的lib路徑
3)--with-x=no: 告訴配置腳本,不要查找 tk (tcl 的 GUI 元件) 或 X 視窗系統庫
4)--with-tclinclude=/home/hanyang/soft/install_tcl/include:指定tcl頭檔案所在目錄,通常指定為tcl安裝目錄下的include路徑
(檢查列印資訊checking for gcc... gcc)
生成Makefile後,修改
CC=/../../ppce6500-hardfloat-linux-gnu-gcc
AR=/../../ppce6500-hardfloat-linux-gnu-ar
[email protected]:/home/hanyang/soft/expect5.45.3# make
[email protected]:/home/hanyang/soft/expect5.45.3# make install
make install會出現錯誤,可以不管,隻要能正常編譯生成expect和libexpect5.45.so即可
編譯生成libexpect5.45.3.so、expect,file看一下是否是運作在PowerPC平台上的。
5.移植
expect -------------\usr\local\bin
libexpect5.45.3.so、libtcl8.6.so-------------\lib
tcl8.6 ------------\usr\lib
調用expect,進入expect環境,說明安裝成功。