通过预期发送两次命令

通过预期发送两次命令

问题描述:

我编写了一个Expect脚本,该脚本登录到远程系统,按顺序执行一些命令并在日志文件中捕获输出。通过预期发送两次命令

除了事实上,一切正在发生,当我检查日志文件时,一些命令似乎被发送两次,以致下一个要发送的命令出现在前一个命令的输出中间。它也会在检测到提示时再次发送(这是正确的执行)。而且,这个问题不会发生在所有情况下,这更令人困惑。

我想补充一点,我已经定制了包含这个“--->”的提示。这是为了更容易由另一个脚本输出解析。

这里的期望码,

set prompt "(]|%|#|>|\\$)" 

# go to bash shell 
expect -re $prompt 
send "/bin/bash\r" 

# customize the prompt 
expect -re $prompt 
send "PS1=\"\\[email protected]\\H ---> \"\r" 

# set new prompt into variable 
expect -re $prompt 
set newPrompt " ---> " 

# opens file containing command list 
set commFile [open commands.txt] 

# reads each line containing commands from file, stores it in "$theLine" variable and sends it. 
while {[gets $commFile theLine] >= 0} { 
    expect "$newPrompt" 
    send "$theLine\r" 
} 

close $commFile 

这是我输出的显示方式。

"prompt --->" command1 
----output---- 
----output---- 
command2 
----output---- 
----output---- 

"prompt --->" command2 
----output---- 
----output---- 

希望你有想法。

我不明白这种行为,也不能在其他地方找到任何解决方案。有任何想法吗?

+0

显示更多代码请 – 2013-03-05 16:00:20

+0

完成,请检查出来,谢谢。 – 2013-03-06 07:06:34

有一点逻辑性问题:在发送PS1=...之后,您会期待旧的提示。然后在循环内部,在发送另一个命令之前,您期待新的提示。这有帮助吗?

send "PS1=\"\\[email protected]\\H ---> \"\r" 
set newPrompt { ---> $} 
expect -re $newprompt 
set commFile [open commands.txt] 
while {[gets $commFile theLine] >= 0} { 
    send "$theLine\r" 
    expect -re "$newPrompt" 
} 
+0

这工作!非常感谢,我没有意识到我期待两次并发送一次:) – 2013-03-07 07:38:05