Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

use Expect

by perldesire (Scribe)
on Aug 07, 2009 at 06:29 UTC ( #786670=perlquestion: print w/replies, xml ) Need Help??
perldesire has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

I want to know the functionality of cpan module Expect. I read here but i could not get what exactly it is doing.

what is the use case for Expect module?

want to know the functionality of this?

and what is the purpose of the expect() in

Any one please give me the simple explanation to understand this.

Replies are listed 'Best First'.
Re: use Expect
by Anonymous Monk on Aug 07, 2009 at 07:08 UTC is built to either spawn a process or take an existing filehandle and interact with it such that normally interactive tasks can be done without operator assistance.

    Expect let your program start another program and pretend human typing into keyboard to talk to another program.

      @Anonymous Monk,

      Thank you so much for your explanation. I have written the below sample program, and I understood the usage of Expect. But I used only 3 to 4 functions of package. has lot of functions to learn.

      As Anonymous Monk said, Expect function, let your program start another program and pretend human typing into keyboard to talk to another program

      In linux, normally if you want to execute a command in a remote machine through ssh, most of us will generate rsa key and use it to login with out asking for password through your scripts.

      Now using the below script (USING EXPECT), we dont have to generate rsa key (secure for sure), you can do ssh with interaction mode and enter information like (confirmation yes/no and password) through expect() from your scripts.

      #! /usr/bin/perl use Expect; my $script = "ssh -x -l root ls"; my $command = Expect->spawn($script); my $pid = $command->pid(); print "PID->\n". $pid; my $password = "computer"; $command->log_stdout(1); my $timeout=7200; #Here you have to give pattern which matches with your messages... while ( $command->expect($timeout, -re => 'want to continue connecting', ## ssh asks con +firmation -re => 'password:', # ssh asks for a password. -re => 'Secure connection to \S+ refused', -re => '^OKc', ) ){ $command->clear_accum(); print "exp_match_number ->".$command->exp_match_number."\n"; if( not defined($command->exp_match_number) ){ print "Error: output not expected, aborting co +nnection\n"; #print "#### String received:\n"; print $command->exp_before, "\n"; $command->hard_close; $retval = 1; print "Retval ->$retval<-\n"; return $retval; } if( $command->exp_match_number == 1 ){ # The authentication was not working, tell ssh + "yes" # we want to connect print $command "yes\r"; next; } elsif( $command->exp_match_number == 2 ){ # Looking for password print $command "$password\r"; next; } elsif( $command->exp_match_number == 3 ){ print $command->exp_before, "\n"; $retval = 0; last; } elsif( $command->exp_match_number == 4 ){ last; } } print "BEFORE....".$command->exp_before; $exp_error = $command->exp_error(); if ( defined ($exp_error) ){ print "Exp Error:$exp_error:\n"; }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://786670]
Approved by wfsp
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2017-03-25 20:04 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (313 votes). Check out past polls.