预期+缓冲区不匹配

时间:2018-09-26 18:58:45

标签: expect

在执行以下代码时,未在我发送df -h命令的地方执行期望第一子句。虽然第二个子句(即密码重置子句)可以正常工作。

#!/usr/bin/expect
set ip [lindex $argv 0]
spawn ssh  anshm@$ip
set timeout 10
match_max 40000
exp_internal -f debug_info.log 0
expect "*word"
send "5t6y%T^Y\n"
expect {
    "localhost*$" {
        send "df -h\r"
        expect eof
    }
    "(curr*assword:" {
        send "5t6y%T^Y\r"
        expect "*word"
        send "7u8i&U*I\r"
        expect "*word"
        send "7u8i&U*I\r"
        expect eof
    }
}

调试信息如下:

1034h[anshm@localhost ~]$ " (spawn_id exp4) match glob pattern "localhost*$"? no
"(curr*assword:"? no
expect: timed out

如果您看到最后一行,则此“ localhost * $”将显示在缓冲区中,但仍显示匹配NO,然后超时。

3 个答案:

答案 0 :(得分:1)

您似乎正在遇到expect全局模式匹配的错误/功能。

为什么输入"...nshm@localhost ~]$ "与全局模式"localhost*$"不匹配? 如果$字符是全局模式中的最后一个字符,则表示输入字符串结束,因此,原则上*应该与localhost之后的绝对值。但是,由于某种原因,模式末尾的*$不能以这种方式工作,但是例如,x$实际上仅在输入的末尾匹配x

但是,您确实想匹配一个文字$字符。为此,请使用\\$。所以您的glob模式应该是

"localhost*\\$"

答案 1 :(得分:0)

@meuh是正确的:即使在全局模式下,跟踪 $也不是文字字符。

$ expect -d <<'END_EXPECT'
spawn sh -c {echo first; echo second localhost other info '$dollar sign '; echo third}
expect "second"
expect {
    {localhost*$}  {puts "found 1: >>$expect_out(0,string)<<"}
    {localhost*$*} {puts "found 2: >>$expect_out(0,string)<<"}
}
END_EXPECT
expect version 5.45
argv[0] = expect  argv[1] = -d
set argc 0
set argv0 "expect"
set argv ""
executing commands from command file
spawn sh -c echo first; echo second localhost other info '$dollar sign '; echo third
parent: waiting for sync byte
parent: telling child to go ahead
parent: now unsynchronized from child
spawn: returns {2595}

expect: does "" (spawn_id exp4) match glob pattern "second"? no
first
second localhost other info $dollar sign
third

expect: does "first\r\nsecond localhost other info $dollar sign \r\nthird\r\n" (spawn_id exp4) match glob pattern "second"? yes
expect: set expect_out(0,string) "second"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) "first\r\nsecond"

expect: does " localhost other info $dollar sign \r\nthird\r\n" (spawn_id exp4) match glob pattern "localhost*$"? no
"localhost*$*"? yes
expect: set expect_out(0,string) "localhost other info $dollar sign \r\nthird\r\n"
expect: set expect_out(spawn_id) "exp4"
expect: set expect_out(buffer) " localhost other info $dollar sign \r\nthird\r\n"
found 2: >>localhost other info $dollar sign
third
<<

我建议您使用正则表达式模式,而不要在全局模式的末尾添加*

expect -re {localhost.*\$} {...}

答案 2 :(得分:0)

谢谢你们的宝贵回答。但是我已经进行了一些调试,发现期望不将其视为字符,因此我仅使用localhost并解决了问题。 但是,如果有人对在变量中分配Expect O / P有任何想法,并将其用于条件语句,例如: 如果{$ variable

欢迎任何有价值的建议。