@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"; }

In reply to Re^2: use Expect by perldesire
in thread use Expect by perldesire

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.