in reply to Re: use Expect
in thread use Expect

@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 Expect.pm package. Expect.pm 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 192.168.1.100 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"; }