Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I'm new to both Perl and "Expect" and recently to automate some prosaic tasks i tried writing a small script(see the snippet below) which tries to log onto a ...
  1. #1
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63

    How to grab stdout when using Expect

    Hi,

    I'm new to both Perl and "Expect" and recently to automate some prosaic
    tasks i tried writing a small script(see the snippet below) which tries to
    log onto a remote machine with a user credentials and issues some commands.
    I need to grab the output of the command executed, how should i go about
    doing that ?

    <sample code>

    #!/usr/bin/perl

    use Expect;

    my ($host,$testUser,$userPasswd) = ('venus','foo','foo123');
    my $timeout = 15;

    $cmd = "cat /etc/passwd";
    my $exp1=Expect->spawn("ssh $testUser\@$host $cmd");
    $exp1->expect($timeout,["password:" => sub {my $self = shift;
    $self->send("$userPasswd\n")}]);
    $exp1->expect($timeout,["\$ " => sub {my $self = shift;
    $self->send("$userPasswd\n")}]);
    my $rc = `echo $?`;
    print "$rc\n";

    <sample code>


    Thanks,
    Amit

  2. #2
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63
    Quote Originally Posted by amit4g View Post
    my $exp1=Expect->spawn("ssh $testUser\@$host $cmd");
    my $exp1=Expect->spawn("ssh $testUser\@$host $cmd 2>tmp.logfile");


  3. #3
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63
    The above change will only put the stderror in a file name
    tmp.logile, In a similar fashion, stdout(whose descriptor number is "1" can be made to logged in a file)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...