in reply to Problem with data retrieved from Net::SSH:perl

That's correct; the Net::SSH::Perl documentation says the first item of the list returned by $ssh->cmd is the entire output of the command.

You can use split to split the output into lines:

my ($out, $err1, $exit2) = $ssh->cmd("df -k"); for my $line (split /\n/, $out) { print "$cnt: $line\n"; $cnt++; }

If the output is potentially large (this doesn't apply for df -k but might be useful to know in the future), and you want to avoid copying all the data like split does you might do something like:

my $cnt = 1; while ($out =~ /\G(.+)\n?|\G\n/g) { print "$cnt: $1\n"; $cnt++; }
or if you're certain the last line ends in a newline, you can simplify the pattern to /\G.*\n/g

And finally it might be more efficient to use index and substr, but it would certainly yield more complex code.

Replies are listed 'Best First'.
Re: Re: Problem with data retrieved from Net::SSH:perl
by runrig (Abbot) on Feb 28, 2003 at 23:37 UTC
    If the output is potentially large...
    If the output from (or the input to) the command is large, there is an experimental, undocumented open2 method (which btrott informed me of while I was writing a file transfer script). It returns tied filehandles, so they won't work as arguments for some things which require real filehandles (like old versions of File::Copy or Net::FTP).
    # I may have in and out reversed here, try both ways my ($out, $in)=$ssh->open2("command and args"); close $in; while (<$out>) { # Process output } close $out;
Re: Re: Problem with data retrieved from Net::SSH:perl
by cosmicsoup (Beadle) on Feb 28, 2003 at 20:17 UTC
    Thanks xmath. That did it. I had read that $ssh->cmd return the entire output as a first line, but I didn't know how to reverse that.

    Many thanks again.