On Tue, Feb 13, 2018 at 1:19 AM Lancelot Mak <lancelot....@computer.org>
wrote:

> #!/usr/bin/perl -W
>
> use SSH::Command;
>
> $cmdln = `grep $ARGV[0] list.txt`;
> chomp($cmdln);
> ($cmdhost,$user,$pass) = split(':',$cmdln);
> $p = `echo $pass|base64 -d`;
> chomp($p);
>
> $cmdlog = ssh_execute(
> host => $cmdhost,
> username => $user,
>
password => $p,, i
>
command => "$ARGV[1]",
> );
>
>
> print $cmdlog;
>
> print "\n";
>

Testing on a Mac OS machine, the I found it consistently prints out 8192
characters.  This should be immediately recognizable as a magic number (8k
or 2**13).  This tells me that SSH::Command (or the underlying libssh2
library) has an 8k buffer and once it is full, it no longer returns any
data.  Looking at the code for SSH::Command, I see the following function:

# Execute command and get answer as text
sub execute_command_and_get_answer {
    my ($ssh2, $command) = @_;

    my $chan = $ssh2->channel();

    $chan->exec($command);
    $chan->read(my $result, 102400);
    chomp $result; # remove \n on string tail

    return $result;
}

This looks like a fixed read (albeit a longer one that I expected, so there
is probably a fixed buffer in libssh2 too), so that is likely the problem.
Changing the function to read until an end of file is detected:

sub execute_command_and_get_answer {
    my ($ssh2, $command) = @_;

    my $chan = $ssh2->channel();

    $chan->exec($command);

    my $result = "";
    until ($chan->eof) {
        $chan->read(my $buf, 4_096);
        $result .= $buf;
    }
    chomp $result; # remove \n on string tail

    return $result;
}

seems to fix the problem.  I am filling a bug against the module, but given
that the last release was in 2009 and the most current version is less than
1.0 (0.7), you may want to find a different module to use.

Reply via email to