in reply to Re: Printing Output From Net::SSH2 exec
in thread Printing Output From Net::SSH2 exec

$chan->shell(); print $chan "ls -la\n"; print "LINE : $_" while <$chan>; print $chan "who\n"; print "LINE : $_" while <$chan>; print $chan "date\n"; print "LINE : $_" while <$chan>;
That is not reliable!

The remote shell doesn't signal in any way when the commands run through it exit to its parent process, the ssh server. If the remote command does not run fast enough, the ... while(<$chan>) loop will finish without having read the full output, and the remaining data will be delayed until the next loop (or even later).

Net::SSH::Expect that uses a similar approach, solves that problem examining the remote output, looking for the shell prompt in order to detect when a subcommand has finished.

Replies are listed 'Best First'.
Re^3: Printing Output From Net::SSH2 exec
by vortura (Initiate) on May 14, 2009 at 19:05 UTC

    Indeed, I've had more success with shell, but as salva points out, occasionally I end up with partial output. The other thing is that it's hard to distinguish stderr from stdout when running a command under shell.

    It seems like Net::SSH2 has a new maintainer, so hopefully that module will start to see some improvement soon. In the meantime, it looks like Net::OpenSSH is the way to go for me.

    R
      Net::SSH2 is working for me. Commands that take a while to return back to shell are fine now:

      my $ssh2 = Net::SSH2->new();
      $ssh2->connect("$host") or die $!;
      my $chan = $ssh2->channel();
      $chan->blocking(1);
      $chan->exec("rpm -qa |wc|awk '{print \$1}'");
      $chan->read($buf, $len);
      my @output;
      while (<$chan>) { push(@output,$_); }
      my $stdout=join ("", @output);
      print $stdout;