paranoid times has asked for the wisdom of the Perl Monks concerning the following question:

I'm putting together a script which should take a users password, and the option of linux or hpux. Then go out to a collection of computers (Either HP or Linux) find one that isn't being used and start some software for the user on it.

At the moment I am able to connect to another computer using expect with an ssh command:
$timeout = 1; $command = '/usr/bin/ssh'; @paramaters = ('servername','pwd'); $exp = Expect->spawn($command, @paramaters) or die "Cannot spawn $command: $!\n"; $exp->expect($timeout, [ qr/password/ ]); $exp->send("password\n"); $exp->soft_close();

This will nicely go through all the steps that I want it to. However I have yet to figure out how to collect data from this effort (In this case the output of 'pwd'). I've tried things like:
$output = $exp->exp_after(); # and $output = $exp->expect($timeout, [ qr/password/ ]); # and $output = $exp->send("password\n");

none of these however seem to put anything into $output
The reason that I want to collect output is to test it to see if the software is running on the system (If it is, it is considered used.)

ssh (the perl module) isn't too viable, as I would need to get it installed on an HPUX system. And I would prefer to avoid that world of excitment for the moment. But who knows.

ssh keys are less viable, as a bunch of random people will be using the script.

Well thank you for any help.
Michael

Replies are listed 'Best First'.
Re: Collecting output from Expect
by rminner (Chaplain) on Oct 17, 2007 at 09:10 UTC
    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
      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
Re: Collecting output from Expect
by shmem (Chancellor) on Oct 17, 2007 at 09:38 UTC
    $output = $exp->exp_after();

    Use

    $output = $exp->exp_before();

    since you want to gather what the remote side sent before it sent the prompt, where expect breaks and yields control.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      I was getting caught up on reading it all backwards for some reason. Thank you for pointing out that bit, I would have been caught up on it for hours.