Ansh
Ansh

Reputation: 1

Expect + Buffer not matching

While executing below code it is not executing expect first clause where I am sending the df -h command. While second clause which is password reset clause is working fine.

#!/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
    }
}

Debug information is below:

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

If you see last line then this "localhost*$" is appears in buffer still it is showing match NO and then timing out.

Upvotes: 0

Views: 517

Answers (3)

Ansh
Ansh

Reputation: 1

Thankyou Guys, for your valuable answers. But i have done some debuging and found that expect not considering it as character so I used localhost only and problem get solved. But If any one has any idea about assigning expect o/p in a variable and use it for condition statement like: If { $variable < Something } { Put "ABCD" } else { Put "DEFG" }

Any Valuable suggestion are welcome in advance.

Upvotes: 0

glenn jackman
glenn jackman

Reputation: 246774

@meuh is right: even in a glob pattern, a trailing $ is not a literal character.

$ 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
<<

I'd recommend using a regex pattern instead of adding * to the end of your glob pattern:

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

Upvotes: 0

meuh
meuh

Reputation: 12255

You seem to be suffering from a bug/feature of expect glob pattern matching.

Why does input "...nshm@localhost ~]$ " not match glob pattern "localhost*$"? When the $ character is the last character in a glob pattern means end-of-input-string, so in principle the * should match absolutely anything following localhost. However, for some reason *$ at the end of the pattern does not work in this way, though, for example, x$ does indeed only match x at the end of input.

However, you really wanted to match a literal $ character. To do this use \\$. So your glob pattern should be

"localhost*\\$"

Upvotes: 1

Related Questions