linux - 期望脚本写入文件部分输出

标签 linux expect

我正在尝试将 expect 脚本 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 的响应和 显示接口(interface) 2 ?

最佳答案

...
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
   }
}

关于linux - 期望脚本写入文件部分输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10078153/

相关文章:

r - 在R的ggmaps上加载 map PNG的栅格数据

python - Python如何获取大文件的实时拷贝进度?

python - 如何在 Redhat 上找到 PYTHONPATH?

linux - Azure Ubuntu : Why is the temp drive mounted to/mnt?

linux - 无法在 Linux 中安装 Expect 实用程序

regex - TCL/EXPECT中正则表达式的含义

linux - 我如何获得昨天的日期

unit-testing - 哪些单元测试框架可用于 x86 汇编器?

telnet - 等待expect脚本中的提示

python - 使用ctypes函数在python中绑定(bind)键事件