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

I am on Windows Vista Home Premium using Perl 5.8 and OpenSSH 5.5.

I have been using 'Net::SFTP::Foreign' for months without any problems then it stopped working. I have been able to trace the problem to the fact that the module uses 'open2' to start the ssh connection but it seems like the 'in' and 'out' handles are not assigned correctly but seem to be assigned to STDOUT and so the module and my script hang waiting for user input when the input is supposed come from my program not the user.

I put a few lines together to see if 'ssh' will work correctly outside of my program. But it doesn't seem to work even in this simple script:

use IPC::Open2; my ($read, $write); my $pid = open2($read, $write, 'C:/Cygwin/bin/ssh -vv -o PreferredAuth +entications=publickey user@domain.com'); if ($@) { print STDOUT "error"; } print $write 'ls'; close $write; my @lines = <$read>; close $read; waitpid $pid, 0; print STDOUT "Output: @lines\n";

I am making a connection to my local Linux box using public key authentication which works fine from the command line.

The above script will hang on the line "my @lines = <$read>;" whereas I would expect it to collect the full output and print it.

Any ideas explanation for this behavior of Perl and ssh?

Replies are listed 'Best First'.
Re: Open2 and ssh problem
by salva (Canon) on Sep 20, 2010 at 16:33 UTC
    That works for my on a Windows XP virtual machine:
    use IPC::Open2; my ($read, $write); my $pid = open2($read, $write, 'ssh -vv 10.0.2.3 -l salva'); syswrite $write, "ls && exit\n"; close $write; while (1) { my $buf; sysread($read, $buf, 1) > 0 or last; print $buf; } close $read; waitpid $pid, 0;
    Cygwin/OpenSSH do very scary things to simulate non-blocking pipes on Windows and sometimes they behave in unexpected ways!