期望脚本速度太快:在从文件读取的每行之间添加睡眠

我正在尝试自动切换命令。 它几乎都是好的,但是当期望脚本从包含我的开关命令的文件(逐行列出)读取每行时,开关似乎在大约10或15个命令后停止,我认为缓冲区太小。

如何在从文件读取的每个命​​令之间添加睡眠? 谢谢!!

set fp [open "/home/room.txt" r] set data [read $fp] set timeout -60 spawn telnet 10.91.60.14 match_max 100000 sleep 2 expect * send -- "^Y" sleep 2 send -- "password\r" sleep 2 send -- "^[\[A" send -- "^[\[A" send -- "\r" sleep 1 send -- "enable\r" send -- "configure terminal\r" sleep 1 expect * sleep 2 **send -- "$data"** sleep 2 interact 

使用while循环:

 set fp [open "datfile"] while {[gets $fp line] >= 0} { puts $line #sleep 3 # but better to wait for the prompt expect # } 

所以,以前的讨论,但经过一两天的expect摔跤,收集用户在这里和其他地方的几个有用的提示,我决定张贴我发现的。 我也在Mac上,而不是原生Linux ,所以有些东西古怪。 这个脚本是从一个bash脚本中调用的:

 expect -d <filename>.expect 

如果#!<PATH to expect> -f被实现在*.expect文件的最上面一行, 并且您:

chmod +x <filename>.expect你的文件,这将工作。 -d用于附加的debugging信息。

调用expectexpect -df <filename>.expect在你的bash脚本中完成同样的效果,你不需要文件的可执行权限。

debugging信息非常有助于查看您的expect语句,variables等,如下所示:

 spawn <program> <args> parent: waiting for sync byte parent: telling child to go ahead parent: now unsynchronized from child spawn: returns {29747} expect: does "" (spawn_id exp10) match glob pattern "Please enter passphrase:"? no Please enter passphrase: expect: does "Please enter passphrase: " (spawn_id exp10) match glob pattern "Please enter passphrase:"? yes expect: set expect_out(0,string) "Please enter passphrase:" expect: set expect_out(spawn_id) "exp10" expect: set expect_out(buffer) "Please enter passphrase:" 

这里是简短的bash脚本(只是一个例子,但是如果你需要它做更复杂的东西,或者只是由于某种原因从bash调用它)

 #!/bin/bash expect -d exp.expect "$WORD" RET=$? if [ $RET -eq 1 ]; then #mac specific, sheer laziness, allows me to do other stuff... #use esay or similar on Linux say "Query was found, expect returned success" echo *************************************************** echo ******************FOUND!*************************** fi exit 0 

这是expect脚本:

 #!/usr/bin/expect -f #capture logs for debugging log_file -a log_file.txt #dont timeout, let the program run its course, (was mandatory for me) set timeout -1; #capture all of our output for debugging set output [ open "output.txt" "RDWR" ]; #This procedure is called repeatedly with the next word proc check_word {w} { #kickoff our other program we are going to talk to spawn <program> <args> #we want this one to go regardless, the next 2 are mutex expect "Please enter passphrase:" { send "$w\r"; send_user "\nSENDING: $w\r"; send_user "\nOutput BUFFER: $expect_out(buffer)\n" } #These are mutually exclusive, either worked or not, can be grouped expect { "scrypt: Passphrase is incorrect" { send_user "\n*FAILED*\n"; close } -re {anders} { send_user "$expect_out(buffer)\n"; close; exit 1 } } #wait for the process to end, timeout is set to never expire wait } #open the file to take the words from, (happens before proc above) set input [ open "words.txt" "RDONLY" ]; #while there are still words, (we exit on a match) ...keep going.... while {[gets $input word] != -1} { check_word $word; } #close input file, TODO do this upon success in expect call too? close $input close $words #EOF 

希望这有助于一些/任何人节省一些时间在那里!