expect的簡單應用

2021-09-23 19:31:45 字數 3236 閱讀 4537

先看3個指令碼:

1.建立使用者:1.createuser.sh

#!/usr/local/bin/expect

##!/usr/bin/expect

set timeout 1

#spawn userdel -r hao

spawn useradd hao

spawn passwd hao

expect

"新的 密碼:

"send

"hao\r

"expect

"重新輸入新的 密碼:

"send

"hao\r

"#spwawn id

spawn ssh localuser@

192.168.77.80

expect

"[email protected]'s password:

"exec sleep

1send

"123\r

"interact

2.登陸普通使用者:2.login.sh

#!/usr/local/bin/expect

##!/usr/bin/expect

set timeout 1

#spawn su -a

spawn ssh hao@

192.168.77.80

expect

"[email protected]'s password:

"exec sleep

1send

"hao\r

"#spawn cd /root/at_einfoworks/svn/components

#spawn /root/at_einfoworks/svn/components/su_target.sh

#expect "$

"#echo who

interact

3.切換其他使用者:su_localuser.sh

#!/usr/local/bin/expect

set timeout 1

spawn su localuser

expect

"密碼:

"sleep

1send

"123\r

"expect "$

"interact

實現了:建立乙個普通使用者,有普通使用者切換到另外乙個普通使用者的功能。

附:linux下安裝expect

見:expect的主頁:

檢查系統,看tcl是否已經安裝。

[root@rhel ~]# rpm -qa|grep tcl

已經安裝的情況:

因為expect需要重新編譯後安裝,那麼就需要tcl的開發包。

rpm -ivh --force tcl-devel-8.4.13-3.fc6.i386.rpm

然後將expect-5.43包解開,開始讀install文件,系統管理員告訴我,不用讀,直接執行 ./configure --help開引數。確實好使,又土了一次 :-( 。

1.首先確定tcl開發包安裝的位置

rpm -qpl tcl-devel-8.4.13-3.fc6.i386.rpm|more

2.然後根據引數,執行./configure

./configure --with-tcl=/usr/lib --with-tclinclude=/usr/include/tcl-private/generic

3.執行

make

4.執行

make install

完成第四步後,expect順利安裝成功。 -

沒有安裝的情況:

第一步,安裝tcl

[[email protected] download]#cd tcl8.4.16/unix/

./configure

make

make install

安裝之後不要刪除原始碼包,在安裝expect時需要tcl的標頭檔案。

安裝完畢以後,進入tcl源**的根目錄,把子目錄unix下面的tclunixport.h copy到子目錄generic中

第二步,安裝tk

[[email protected] download]#cd tk8.4.16/unix/

./configure

make

make install

第三步,安裝expect

在不提供引數的情況下執行configure報錯:

checking for tcl private headers... checking for tclint.h... no 

configure: error: can't find tcl private headers 

再次執行configure指令碼,並指定tcl的標頭檔案所在目錄:

[[email protected] expect-5.43]#./configure --with-tclinclude=../tcl8.4.16/generic

指令碼執行正常,進行執行make進行編譯

make

如果make,或是configure有錯誤的話,根據提示,再增加配置引數

編譯過程中未出現錯誤,執行安裝:

make install

配置引數的意思是:

·--with-tcl=/usr/tcl/lib :(我的環境中是/usr/local/lib) 確保配置指令碼找到臨時工具目錄中的tcl ,我們不希望它使用主系統中可能存在的tcl.

·--with-x=no : 告訴配置指令碼,不要查詢 tk (tcl 的 gui 元件) 或 x 視窗系統庫,這兩個都有可能存在於主系統中。

·-with-tclinclude :幫助指令碼找到所需要的tcl 標頭檔案。

簡單一點就是:

1.安裝tcl8.4.16

cd tcl8.4.16/unix/./configure;make;make install

cp tclunixport.h ../generic/

2.安裝tk

cd tk8.4.16/unix

./configure;make;make install

3.安裝expect

./configure --with-tclinclude=../tcl8.4.16/generic;

make;make install

expect的簡單應用

先看3個指令碼 1.建立使用者 1.createuser.sh usr local bin expect usr bin expect set timeout 1 spawn userdel r hao spawn useradd hao spawn passwd hao expect 新的 密碼 ...

expect的簡單應用

先看3個指令碼 1.建立使用者 1.createuser.sh usr local bin expect usr bin expect set timeout 1 spawn userdel r hao spawn useradd hao spawn passwd hao expect 新的 密碼 ...

expect 的小點 expect的使用

expect password send 123456 r 注意 expect 和字串 password 中間有乙個空格,send和字串 123456 r 中間有乙個空格。因為expect和send都是乙個命令,只有加空格才能與引數分開。expect 注意 expect與 之間有乙個空格,因為這樣e...