0

我正在尝试将期望脚本 ssh-ing 到服务器的部分输出写入日志文件。
现在我有这个脚本:

#!/usr/bin/expect -f
spawn telnet 192.168.20.222
match_max 10000
expect "*?to continue*"
send -- "\r"
send -- "show interfaces 1 \r"
expect -- "*?2626#*"
send -- "show interfaces 2 \r"
expect -- "*?2626#*"
send -- "exit \r"
expect -- "*?2626>*"
send -- "exit \r"
expect "*?y/n*"
send -- "y \r"

我怎样才能将其更改为仅输出到文件“log.txt”show interfaces 1和 来自的响应show interfaces 2

4

1 回答 1

1
...
log_file "log.txt" ;#enable loging to file as well
send -- "show interfaces 1 \r"
expect -- "*?2626#*"
send -- "show interfaces 2 \r"
expect -- "*?2626#*"
log_file; #disable logging
...

'log.txt' 将包含两个命令 + 命令本身 + 提示的输出。这足够了吗?

获得纯输出可能更复杂,有用的技术是使用显式括号(不确定您正在与之交互的系统是否允许类似的东西)。在 shell 中,它看起来像:

send -- "show interfaces 1\r echo '<XYZ>'\r"
expect "<XYZ>"; #first echoed when the command was typed
expect {
   "<XYZ>" {
        #for the second time echoed when the command finished
        #here the $expect_out(0,string) contains the command output + the bracket
        regexp {(.*)<XYZ>} $expect_out(0,string) match pure_cmd_output
   }
}
于 2013-02-20T11:20:39.593 回答