当我发送的命令太长而无法放在一行上时,Expect的行为并不像预期的那样

时间:2013-07-16 22:14:20

标签: tcl expect

我正在尝试创建一个expect脚本,它将grep一个文件并返回包含我正在寻找的字符串的行,在这种情况下,该字符串将是一个终端ID。例如,我有一个名为terminal_list.txt的文件,其中包含以下内容:

 0x400 192.168.62.133 10006
 0x420 192.168.62.133 10021
 0x440 192.168.62.133 10022

我希望返回的行以0x420开头

我的代码如下:

    #!/usr/bin/expect -f

set terminal_list "/home/linkway/terminal_list.txt"
set termid "0x400"

spawn /bin/bash

expect "] "

    # Get ip and port of terminal
    # Check if termid exists in terminal_list file
    set command "grep -q '$termid' '$terminal_list' && echo 'true' || echo 'false'"
    send "$command\r"
    expect "$command\r\n"
    expect -re "(.*)\r\n.*] "
    set val $expect_out(1,string)
    # If terminal_list does not exist print error and exit
    if { [string first "No such file or directory" $val] >= 0 } {
      puts "$terminal_list does not exist. Script Failed"
      exit
    # If terminal_list does not contain termid print error and continue
    } elseif { [string match "false" $val] } {
      puts "\nTerminal $termid does not exist in ${terminal_list}. Cannot update bootfile.\n"
    # If termid is found in terminal_list, get the ip and port of the terminal
    } else {
      set command "grep '$termid' '$terminal_list'"
      send "$command\r"
      expect "$command\r\n"
      expect -re "(.*)\r\n.*] "
      set val $expect_out(1,string)
      set ip_port [string range $val 6 end]
    }

当我通过putty ssh到RHEL服务器并在最大化的putty窗口中运行脚本时,这非常有效。但是,当我缩小窗口长度以使grep命令不再适合单行时,我的代码会中断!有人可以帮我提出解决方案吗?我一直在努力处理expect_out,并且可以真正使用一些指导。

编辑:我找到了造成这个错误的原因。事实证明,当grep命令分成多行时,\ r \ n被添加到换行符所在的命令中。以下是exp_internal 1的一些调试信息。您可以看到如何将\ r \ n添加到命令运行到下一行的grep命令中:

expect: does "grep -q '0x400' '/home/linkway/term \rinal_list.txt'
&& echo 'true' || echo 'false'\r\n" (spawn_id exp6)
match glob pattern "grep -q '0x400' '/home/linkway/terminal_list.txt'
&& echo 'true' || echo 'false'\r\n"? no

为什么会发生这种情况?获取grep命令输出的正确方法是什么?我发现很奇怪,根据命令输出在屏幕上的显示方式,预期会有不同的表现。非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

通过让我的脚本更具期待性,我能够找到更清晰的问题解决方案。这是它的样子:

set command "grep -q '$termid' '$terminal_list' && echo 'true' || echo 'false'"
send "$command\r"
expect {
  -re ".*\r\ntrue\r\n.*] " {
    send "grep '$termid' '$terminal_list'\r"
    expect ".*\r\n"
    expect -re "(.*)\r\n.*] "
    set val $expect_out(1,string)
    set ip_port [string range $val 6 end]

    puts "\nUpdating $termid bootfile"
    updatebootfile $ip_port $boot_data $termid
  }
  -re ".*\r\nfalse\r\n.*] " {
    puts "\nTerminal $termid does not exist in ${terminal_list}. Cannot update bootfile.\n"
  }
  -re "No such file or directory.*] " {
    puts "$terminal_list does not exist. Script Failed"
    exit 1
  }
  timeout {
    puts "expect timeout when searching for $termid in $terminal_list"
  }
}