0

我编写了一个登录到远程系统的 Expect 脚本,按顺序执行一些命令并将输出捕获到日志文件中。

一切都很好,除了当我检查日志文件时,一些命令似乎被发送了两次,这样下一个要发送的命令出现在前一个输出的中间。它还会在检测到提示时再次发送(这是正确的执行)。此外,这个问题并非在所有情况下都会发生,这更加令人费解。

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

这是期望的代码,

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

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

# customize the prompt
expect -re $prompt
send "PS1=\"\\u@\\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----

希望你能明白。

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

4

1 回答 1

1

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

send "PS1=\"\\u@\\H ---> \"\r"
set newPrompt { ---> $}
expect -re $newprompt
set commFile [open commands.txt]
while {[gets $commFile theLine] >= 0} { 
    send "$theLine\r"
    expect -re "$newPrompt"
}
于 2013-03-06T12:02:08.200 回答