Posted on 2006-01-20 20:40:59-08 by manuel

Hi, I usually use expect_after and expect_before with expect to control timeout's and eof in my scripts:

  1. ! /usr/bin/expect
  2.  
  3. spawn ssh manuel@linux01
  4. set timeout 2
  5.  
  6. expect_before {
  7. timeout {puts "timeout before"; exit}
  8. }
  9. expect_after {
  10. eof {puts "eof after"; exit}
  11. }
  12.  
  13.  
  14. expect {
  15. "assword: " {send "password"}
  16. }
  17. ...

I tried something similar with expect.pm, but it doesn't work:

  1. use strict;
  2. use Expect;
  3.  
  4. my $exp = new Expect();
  5.  
  6.  
  7. my @param=qw(root@linux);
  8. $exp->spawn("ssh",@param) || die ("error en comando: $! \n");
  9.  
  10. $exp->exp_before( 'timeout', \&timeout);
  11.  
  12. $exp->exp_after( 'eof', \&eof);
  13.  
  14. $exp->expect(2,'-re',"bssword");
  15. ...
  16.  
  17. sub timeout {
  18. print "algo por timeout\n";
  19. exit;
  20. }
  21.  
  22. sub eof {
  23. print "algo por eof\n";
  24. exit;
  25. }

Why exp_before don't work like expect_before? Regards

Posted on 2006-02-06 10:34:12-08 by rgiersig in response to 1669

Short answer: I didn't need exp_before and exp_after (I prefer to use the callback style and write out all the possibilities explicitely), so I didn't invest anything into the code (just took the code that Austin had written).

How about writing it like this:

  1. $exp->expect(2,
  2. [ "assword", sub { $exp->send("$password\r\n"); } ],
  3. [ 'timeout', \&timeout ],
  4. [ 'eof', \&eof ],
  5. );

Posted on 2006-03-04 16:36:31-08 by manuel in response to 1760

It's not what I want, but it works. thank's

(This article is based on a thread on the CPAN::Forum.)