Expect 脚本的 grep 输出

Expect 脚本的 grep 输出

我正在尝试运行一些期望语言,并在一个脚本中 grep/parse 输出。我想 grep 的输出并查找“错误”(我应该注意,像 awk、sed、grep 等标准 Linux 命令在远程 VPlexcli 计算机上不可用)

#!/bin/bash

expect - << EOF
# connect to vplexcli
spawn vplexcli
# Look for login prompt
expect -re "Name:"
# Send login
send "service\r"
# Look for password prompt
expect -re "Password:"
# Send password
send "letmein123\r"
expect -re "VPlexcli:/> "
send "ll /clusters/cluster-1/storage-elements/\r"
expect -re "VPlexcli:/> "
send "exit\r"
EOF

输出看起来像这样:

VPD83T3:6006016036c02c00e217465c0516e211  ok            APM00121002844.SPA  APM00121002844.SPB  both        0x002e000000000000  implicit-explicit
VPD83T3:6006016036c02c00e4dc0671f907e211  ok            APM00121002844.SPA  APM00121002844.SPB  both        0x0010000000000000  implicit-explicit
VPD83T3:6006016036c02c00ec79619bdd08e211  error         APM00121002844.SPA  APM00121002844.SPB  none                            implicit-explicit
VPD83T3:6006016036c02c00f0bfd3dedd08e211  error         APM00121002844.SPA  APM00121002844.SPB  none                            implicit-explicit

答案1

您可以捕获输出并在预期内解析它

#!/usr/bin/expect -f

# connect to vplexcli
spawn vplexcli
# Look for login prompt
expect -re "Name:"
# Send login
send "service\r"
# Look for password prompt
expect -re "Password:"
# Send password
send "letmein123\r"
expect -re "VPlexcli:/> "
send "ll /clusters/cluster-1/storage-elements/\r"
expect -re "(.*)VPlexcli:/> "

foreach line [split $expect_out(1,string) \n] {
    if {[string match *error* $line]} {
        puts $line
    }
}

send "exit\r"
expect eof

答案2

这是你想要的吗?

#!/bin/bash

(expect - << EOF

...

EOF
) | while read line; do if [[ "$line" =~ "error" ]]; then  echo $line; fi; done

很惊讶你没有 grep。

相关内容