Monday, March 22, 2010

Autoexpect Scenario

Autoexpect固然方便,但是很多時候還是必須手動改正Autoexpect所錄製過的script才能順利運行,以下將透過SSH登入本機後執行vmstat的指令來說明:

1. 執行Autoexpect:
# autoexpect
此時系統會產生一個autoexpect的shell環境,並將使用者的動作紀錄起來,離開時請輸入exit指令,之後系統會產生個script.exp的紀錄檔(By default)。

2. 執行script.exp:
lawrence@X60:~/Desktop> ./script.exp
spawn /bin/bash
lawrence@X60:~/Desktop> ssh root@127.0.0.1
Password:
此時會發覺停留在輸入Password:而不再有進一步的動作,這是為何呢?

3. 查看並修正script.exp:
原始script.exp(僅截取錄製的部份):
set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "]2;lawrence@X60:~/Desktop]1;X60lawrence@X60:~/Desktop> "
send -- "ssh root@10"
expect -exact [K"
send -- ""
expect -exact [K"
send -- "127.0.0.1\r"
expect -exact "127.0.0.1\r
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.\r
RSA key fingerprint is fa:19:31:7a:ac:04:41:af:4c:38:b7:58:16:a3:14:f8.\r
Are you sure you want to continue connecting (yes/no)? "
send -- "yes\r"
expect -exact "yes\r
Warning: Permanently added '127.0.0.1' (RSA) to the list of known hosts.\r\r
Password: "
send -- "1234\r"
expect -exact "\r
Last login: Fri Mar 12 17:06:12 2010\r\r
Have a lot of fun...\r
[1m[31mX60:~ # [m"
send -- "vmstat -n 1 5\r"
expect -exact "vmstat -n 1 5\r
procs -----------memory---------- ---swap-- -----io---- -system-- -----cpu------\r
r b swpd free buff cache si so bi bo in cs us sy id wa st\r
14 0 25644 20660 26124 464024 0 1 17 23 881 255 7 3 89 1 0\r
0 0 25644 20148 26124 464536 0 0 0 0 1725 2206 6 2 93 0 0\r
0 0 25644 20404 26124 464264 0 0 0 0 1877 2234 3 3 95 0 0\r
0 0 25644 20404 26124 464224 0 0 0 0 1736 2186 4 1 95 0 0\r
0 0 25644 20404 26124 464224 0 0 0 0 1628 2168 3 2 95 0 0\r
[1m[31mX60:~ # [m"
send -- "ex"
expect -exact [K"
send -- ""
expect -exact [K"
send -- "logout\r"
expect -exact "logout\r
Connection to 127.0.0.1 closed.\r\r
]2;lawrence@X60:~/Desktop]1;X60lawrence@X60:~/Desktop> "
send -- "exit\r"
expect eof


問題1:
The authenticity of host '127.0.0.1 (127.0.0.1)' can't be established.\r
RSA key fingerprint is fa:19:31:7a:ac:04:41:af:4c:38:b7:58:16:a3:14:f8.\r
Are you sure you want to continue connecting (yes/no)? "
預設SSH Server/Client連接時,會去紀錄對方的金鑰,也就是因為這個原因,讓expect沒有得到預期的輸出,因為SSH第n次連線後就不需要紀錄彼此的金鑰了。

問題2:
Last login: Fri Mar 12 17:06:12 2010\r\r
Have a lot of fun...\r
^[\[1m^[\[31mX60:~ # ^[(B^[\[m"
每次登入系統的時間一定會是不一樣的,所以把它也註解掉。

問題3:
procs -----------memory---------- ---swap-- -----io---- -system-- -----cpu------\r
r b swpd free buff cache si so bi bo in cs us sy id wa st\r
14 0 25644 20660 26124 464024 0 1 17 23 881 255 7 3 89 1 0\r
0 0 25644 20148 26124 464536 0 0 0 0 1725 2206 6 2 93 0 0\r
0 0 25644 20404 26124 464264 0 0 0 0 1877 2234 3 3 95 0 0\r
0 0 25644 20404 26124 464224 0 0 0 0 1736 2186 4 1 95 0 0\r
0 0 25644 20404 26124 464224 0 0 0 0 1628 2168 3 2 95 0 0\r
^[\[1m^[\[31mX60:~ # ^[(B^[\[m"
當然不需要上一次vmstat的輸出結果,所以也需要註解掉。

修改過後:
set timeout -1
spawn $env(SHELL)
match_max 100000
expect -exact "]2;lawrence@X60:~/Desktop]1;X60lawrence@X60:~/Desktop> "
send -- "ssh root@10"
expect -exact [K"
send -- ""
expect -exact [K"
send -- "127.0.0.1\r"
expect -exact "Password: "
send -- "1234\r"
expect -exact "#"
send -- "vmstat -n 1 5\r"
expect -exact "#"
send -- "ex"
expect -exact [K"
send -- ""
expect -exact [K"
send -- "logout\r"
expect -exact "logout\r
Connection to 127.0.0.1 closed.\r\r
]2;lawrence@X60:~/Desktop]1;X60lawrence@X60:~/Desktop> "
send -- "exit\r"
expect eof

4. 執行:
lawrence@X60:~/Desktop> ./script2.exp
spawn /bin/bash
lawrence@X60:~/Desktop> ssh root@127.0.0.1
Password:
Last login: Mon Mar 22 16:11:29 2010 from localhost
Have a lot of fun...
X60:~ # vmstat -n 1 5
procs -----------memory---------- ---swap-- -----io---- -system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 31016 27124 23500 454188 0 1 16 23 880 303 7 3 89 1 0
0 0 31016 29204 23500 451648 0 0 0 20 1815 1834 2 3 95 0 0
0 0 31016 29204 23500 451676 0 0 0 0 1821 1878 2 2 96 0 0
0 0 31016 29700 23500 451232 0 0 0 0 1763 2029 3 2 95 0 0
0 0 31016 29700 23500 451184 0 0 0 0 1807 2241 2 2 95 0 0
X60:~ # logout
Connection to 127.0.0.1 closed.
lawrence@X60:~/Desktop> exit
exit
lawrence@X60:~/Desktop>
Autoexpect雖然有以上的小缺點,但還是能加快script開發的速度。

No comments: