ActiveState Community

Problem Expect

Posted by douglas_aparicio_bermudez on 2009-09-21 12:55

Greetings.

I have a problem with expect of tcl, when enter a server.
My script is thus:

#!/usr/bin/expect

package require Expect

set pid [spawn telnet 10.10.10.17]

set id $spawn_id

expect -i $id timeout {

    puts "timeout in user login"

    } eof {

    puts "spawn failed with eof on login"

    } -re "login:.*" {

    exp_send -i $id -- "admin\r"

    }

expect -i $id timeout {

    puts "timeout in user password"

    } eof {

    puts "spawn failed with eof on password"

    } -re "Password:.*" {

    exp_send -i $id -- "_adm1n_\r"

    }

   

expect -i $id timeout {

    puts "timeout in user local"

    } eof {

    puts "spawn failed with eof on local"

    } -re "\[local\]SE100#" {

    exp_send -i $id -- "context switched\r"

    }

exp_close -i $id

#!/usr/bin/expect


package require Expect



set pid [spawn telnet 10.10.10.17]

set id $spawn_id



expect -i $id timeout {

    puts "timeout in user login"

    } eof {

    puts "spawn failed with eof on login"

    } -re "login:.*" {

    exp_send -i $id -- "admin\r"

    }



expect -i $id timeout {

    puts "timeout in user password"

    } eof {

    puts "spawn failed with eof on password"

    } -re "Password:.*" {

    exp_send -i $id -- "_adm1n_\r"

    }

    

expect -i $id timeout {

    puts "timeout in user local"

    } eof {

    puts "spawn failed with eof on local"

    } -re "\[local\]SE100#" {

    exp_send -i $id -- "context switched\r"

    }





exp_close -i $id

And I cannot capture with expect this chain "[local]SE100#"

I will be thankful for some aid

--------
Douglas Aparicio Bermudez.

jhescola | Tue, 2009-10-06 06:42

Hi Aparicio,
Are you suer that you have to expect regular expression instead of string directly?
Are those [ ] # chars in the string or they are placeholders...
I think you should do:

puts "spawn failed with eof on local"

} "\[local\]SE100#" {

exp_send -i $id -- "context switched\r"

instead.
Regards
Jorge

niobe | Tue, 2009-10-06 16:32

You have to remember that angle brackets have special meaning to both TCL and the expect pattern matcher.

So "\[" means TCL treats it as a literal pattern.
But the pattern matcher treats it a list of characters l,o,c,a,l

What you actually want is:

\\\[local\\\]SE100#

to match a literal "[local]"

This is covered in 'Exploring Expect'