TASdvlper has asked for the wisdom of the Perl Monks concerning the following question:

All,

I'm new to using Perl's Expect module so maybe I'm going about this the wrong way. Basically, with the code below i'm trying to login, run a script and exit. But, because the 1st thing that is returned from a telnet is "Trying aaa.bb.ccc.ddd ... " and my script gets stuck in a endless loop. Any pointers ?

use strict; use warnings; use Expect; $Expect::Exp_Internal = 1; my $my_login = "xxx"; my $my_password = "yyy"; my $timeout = "5"; #Spawn a Telnet Process to connect to a build machine. my $exp = Expect->spawn("telnet aaa.bb.ccc.ddd") or die "Cannot Spawn Telnet...exiting\n"; #Send user name a password. $exp->expect($timeout, [ qr/^login:\s+$/i, sub { my $self = shift; $self->send("$my_login\n"); exp_continue; } ], [ qr/^password:\s+$/i, sub { my $self = shift; $self->send("$my_password\n"); exp_continue; } ], # [ qr/.*/i, sub { my $self = shift; # $self->send("/sean/test.pl\n"); # i exp_continue; } ], [ qr/^COMPLETE.*$/i, sub { my $self = shift; $self->send("exit\n"); exit; } ], [ qr/.*/i, sub { my $self = shift; exp_continue; } ], );

Replies are listed 'Best First'.
Re: Need some pointers using Expect.pm
by waswas-fng (Curate) on Dec 03, 2003 at 23:16 UTC
    Why not use Net::Telnet instead? it is specialized for the tasks that you are trying to perform.


    -Waswas
      Funny you ask. I'm actually researching what's better, Expect or Telnet (which is the original post that triggered this post). I've heard agreements that both methods are good, but there are more dependancies regarding telnet.
        Seems easy to me...
        use Net::Telnet (); $t = new Net::Telnet (Timeout => 10, Prompt => '/bash\$ $/'); $t->open("sparky"); $t->login($username, $passwd); @lines = $t->cmd("who"); print @lines;
        although I would use ssh if at all available. Expect is too generic for this task. You have to rewrite all of the status tests for the login/disconnect and all the other stuff like what if the telnet bin segfaults while you are running our control script. Net::Telnet was made for this task and is damn good at doing it.


        -Waswas