【问题标题】:expect script write to file partial output期望脚本写入文件部分输出
【发布时间】:2013-02-20 19:20:39
【问题描述】:

我正在尝试将期望脚本 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?

【问题讨论】:

    标签: linux expect


    【解决方案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
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      • 2013-04-18
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      相关资源
      最近更新 更多