in reply to Collecting output from Expect

Hi, a timeout of 1second is very little. ssh can sometimes take longer. Another thing is that the execution of sending the password should depend on a string having matched. It shouldn't be executed no matter what. Also: always use strict; use warnings; ...
A small working example:
use strict; use warnings; use Expect; my $timeout = 5; my $command = '/usr/bin/ssh'; my $user='johndoe'; my $host='192.168.47.11'; my $password = 'godsexpower'; my $exp = Expect->spawn($command, "$user\@$host") or die "Cannot spawn $command: $!\n"; $exp->expect($timeout, [ qr{password:} , sub {$exp->send("$password\n" +)} ]); $exp->expect($timeout, [ qr{\$ } , sub {$exp->send("ls\n")} ]); $exp->expect($timeout, [ qr{\n*?\$ } , sub {print "got the output: '". + $exp->before()."'\n"} ]); $exp->soft_close();
This example expects the prompt of a host to end on '$ ' (DollarSpace), you might have a diffrent prompt(especially if the HPux box uses csh, then it's usually '> '). Hope this helps. Cheers Roland

Replies are listed 'Best First'.
Re^2: Collecting output from Expect
by paranoid times (Acolyte) on Oct 18, 2007 at 04:26 UTC
    I got the jist of it done and happily went home. And I was coming back to when I noticed some other points that you brought up. I still need to put something in for different shells...Though it might be better to just dumb it down and not pattern match at all. I'll go after that one tomorrow.

    Thank you very much for your help.
    Michael