Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Net::SSH::Perl doesn't like long commands

by pattyj (Novice)
on Jun 10, 2013 at 11:12 UTC ( [id://1038048]=perlquestion: print w/replies, xml ) Need Help??

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

I've asked this on their mailing list, but it's not very active, so posting here... My Net::SSH::Perl-using script hangs for at least five minutes (maybe forever?) when the number of characters in the command is large.
#!/usr/bin/perl use strict; use Net::SSH::Perl; my $host = "blah"; my $ssh = Net::SSH::Perl->new( $host, debug => 1, options => [ "BatchMode yes", "PasswordAuthentication no", "ConnectTimeout 15", ] ); $ssh->login("root") or die "Failed to login: $!\n"; my $cmd = "echo "; $cmd = "."x50000; my ($stdout, $stderr, $exit) = $ssh->cmd($cmd); die "Failed!\n" unless ($exit == 0); print "$stdout\n";
Chang 50000 to 20000 and it will work. Same problem on all hosts I tested against. Can anyone help me understand why?

Replies are listed 'Best First'.
Re: Net::SSH::Perl doesn't like long commands
by salva (Canon) on Jun 10, 2013 at 12:14 UTC
    This looks like a bug on Net::SSH::Perl.

    I can reproduce the problem myself. In my environment, Net::SSH::Perl hangs at some point between 8000 and 10000 characters.

    Net::OpenSSH can handle the same commands without any issue (that's why I think the problem lies on Net::SSH::Perl):

    #!/usr/bin/perl use strict; use Net::OpenSSH; use Net::SSH::Perl; my $host = $ARGV[0] // "localhost"; for my $len (10, 100, 1000, 2000, 4000, 6000, 8000, 10000, 20000, 5000 +0) { my $cmd = "echo "; $cmd .= "." x $len; warn "trying with Net::OpenSSH, len=$len...\n"; my $ssh1 = Net::OpenSSH->new($host); my ($stdout1, $stderr1) = $ssh1->capture($cmd) or die "Failed!\n"; warn "Failed $?!\n" if $?; warn "trying with Net::SSH::Perl, len=$len...\n"; my $ssh2 = Net::SSH::Perl->new( $host, # debug => 1, options => [ "BatchMode yes", "PasswordAuthenticati +on no", "ConnectTimeout 15" ] + ); $ssh2->login("root") or die "Failed to login: $!\n"; warn "logged\n"; my ($stdout2, $stderr2, $exit) = $ssh2->cmd($cmd); warn "Failed $exit!\n" if $exit; }
      hangs at some point between 8000 and 10000 characters.

      What's the betting that it is somewhere around 8192, the size of PerlIO IO buffers since 5.1(2|4|?).

      Maybe someone how has reproduced it could try disabling the buffering and see if that changes anything.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I see syswrite calls in this code

        http://cpansearch.perl.org/src/SCHWIGON/Net-SSH-Perl-1.35/lib/Net/SSH/Perl/Packet.pm

        Looks like it's not protected from EAGAIN and from case when syswrite didn't write all data.

        sysread calls are protected.

        It is very unlikely that buffering may be the root of the problem because of the way the SSH protocol is internally structured in several layers. A buffering issue would have manifested itself as a wider set of problems.
        Setting $|++ doesn't help unfortunately (and the value on my boxes are different, see above... :/ ).
Re: Net::SSH::Perl doesn't like long commands
by syphilis (Archbishop) on Jun 10, 2013 at 14:01 UTC
    Net::SSH::Perl doesn't like long commands

    Net::SSH::Perl has a history of not being user-friendly.
    Fer chrissake just use one of salva's SSH modules - whichever one is right for your OS , and ask if you don't know which one that is.
    (salva is far too modest to state that explicitly, but I'm not bound by any such constraints.)

    I mean no offence to the current maintainer (yes, it apparently now has one) of Net::SSH::Perl, but a module that contains the word "Perl" in its name yet has so many C-extensions as pre-requisites, is surely trying to sell itself on false pretences.

    Cheers,
    Rob
      I am not so modest. The thing is more on the I-am-tired-of-recommending-my-modules side :-)

      My usual recommendation is to go for Net::OpenSSH if you are on a Linux/Unix box and Net::SSH2 (currently being maintained by Rafael Kitover) if you are on Windows.

      Net::SSH::Any is supposed to provide an unified interface to both modules but it is still a work in progress. Though, it is already been used, and what it does, it does it right, mostly!

        and Net::SSH2 (currently being maintained by Rafael Kitover) if you are on Windows

        And many thanks to Rafael for that module - I'm on MS Windows, and that's the precise module I use. (Of course, I use it via Net::SSH2::Foreign::Backend::Net_SSH2 ... not sure where *that* module comes from ;-)

        Cheers,
        Rob
        and Net::SSH2 (currently being maintained by Rafael Kitover) if you are on Windows

        And many thanks to Rafael for that module - I'm on MS Windows, and that's the precise module I use. (Of course, I use it via Net::SSH2::Foreign::Backend::Net_SSH2 ... not sure where *that* module comes from ;-)

        UPDATE: Ooops ... I'm confusing SFTP and SSH2, now. Probably time for a nap;-)

        Cheers,
        Rob
Re: Net::SSH::Perl doesn't like long commands
by hdb (Monsignor) on Jun 10, 2013 at 11:24 UTC

    • Have you tried to issue the command in an interactive shell? Will it also work for 20000 and fail for 50000?
    • What is the exact number that makes it fail? Try around 32768 which is the only power of two in the range of 20000 to 50000.

    • Issuing the command in an interactive shell works fine.
    • The script fails when length($cmd) is > 23117.
        With $host as "localhost", the script works (but fails sshing to an identical machine).
Re: Net::SSH::Perl doesn't like long commands
by renormalist (Sexton) on Aug 09, 2013 at 22:46 UTC

    I just uploaded v1.36 to CPAN which contains several bugfixes with longer history. Please try if your problem is covered.

    Net::SSH::Perl is in maintenance mode. I accept patches but can't dive into debugging on my own.

    Kind regards,
    Steffen

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1038048]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-03-19 08:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found