2014-04-29 128 views
0

命令我新的希望。我試圖在循環中運行一個命令。Expect腳本來運行循環

  1. SSH連接到一臺機器
  2. 執行相同的命令多次
package require Expect 

set uname admin 
set password default 
set hostname "182.35.44.35" 

#SSH into Appliance 
set timeout 10 
cd /cygwin/bin 
puts "changed into directory and exec telnet" 
spawn telnet "$hostname" 

expect "br" 
exp_send "admin\r" 
expect "Password" 
exp_send "******\r" 
expect "br" 
exp_send "zzdebugshell\r" 
after 1000 
send "*******\r" 
after 1000 
send "*******\r" 
expect "debugshell#" 
after 1000 

set times 0; 
while { $times < 10 } 
{ 
send "vmstat -n 2 5\r" 
set times [expr $times+1]; 
} 

expect "debugshell" 
exp_send "exit\r" 

expect "br" 
exp_send "exit\r" 
exit 

Error : debugshell# child process exited abnormally

能否請你幫我這個?

回答

0

本節着眼錯誤:

set times 0; 
while { $times < 10 } 
{ 
send "vmstat -n 2 5\r" 
set times [expr $times+1]; 
} 

你應該是send後做一個expect讓您等待的提示來之前再次兜兜循環。你的循環也是非慣用的,把大括號放在換行符上不會與Tcl一起工作(因爲換行符是命令終止符,除非引用)。

校正/改進的版本應該是:

for {set times 0} {$times < 10} {incr times} { 
    send "vmstat -n 2 5\r" 
    expect "debugshell" 
} 

您還希望能夠在循環後,立即放棄了等待的提示,因爲它現在是內循環。當然。

你也應該(也許)之前把close和(絕對)一waitexit

exp_send "exit\r" 
# Maybe 'expect' something here... 
close 
wait 
exit 

(那些expect "br"線看起來很奇怪我,但也許他們是OK的,我不知道你的提示看起來像什麼......)