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

hi i am trying to write a script which executes a command on a remote server and captures the output in a variable .I was able to execute the command but unable to capture the output and it is directed to standard output.
here is the code

use Expect; my $exp = new Expect; $exp->raw_pty(0|1); $exp->log_stdout(0); $exp->spawn("ssh","servername","ls /"); $exp->expect(2,"-re","connecting"); $exp->send("yes\n"); $exp->expect(10,"-re","password"); $exp->send("password\n"); $exp->expect(0); $read = $exp->before(); $exp->soft_close(); $exp->log_stdout(1); print $read;

Replies are listed 'Best First'.
Re: perl Expect
by betterworld (Curate) on Aug 15, 2008 at 13:50 UTC

    You can use SSH with public key authentication, which removes the need for typing a password, thus making ssh much more scriptable. Maybe you have reasons for wanting a password, but just in case: How to set up ssh with public key authentication

      yeah... what you said is absolutely correct...rsa authentication is the best way to do it....
      but what i am thinking is if one should propagate keys to a large number of servers then though less secure the best thing that came to my mind is to write an expect script to propagate the keys....
      As this is insecure i would like to know better ways to do it
Re: perl Expect
by Illuminatus (Curate) on Aug 15, 2008 at 23:08 UTC
    Did you look at Net::SSH::Expect? I have not used it personally, but it does have methods for collecting the input into strings. I assume by 'output' you really mean input. You obviously know what you are outputting :)