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

Hi Folks,

I've been searching around for solutions to my questions and have ended up at Perl Monks several times. So I figure this is the place to ask!

Essentially running this code returns inconsistent results. Sometimes I'll get 113 items, sometimes 102, sometimes 0. The returned output should be an md5 sum of each file on the server.

#!/usr/bin/perl use Net::SSH2; $ssh2 = Net::SSH2->new(); $ssh2->connect('asdfasdfas.com') or die $!; $ssh2->auth_password('user','pass') or die "Unable to login $@ \n"; $ssh2->debug(0); $chan2 = $ssh2->channel(); $chan2->blocking(0); $chan2->shell(); print $chan2 "bash ~/md5xml\n"; print "LINE : $_" while <$chan2>; $chan2->close; print 'done...';
Also the script md5xml runs

find ./public_html -type f -exec md5sum {} \;

I would have type this into exec directly, but I couldn't figure why my \ wasn't escaping properly.

Also somebody suggested running this code

$l=<$chan2>, print "LINE : $l" while !eof($chan2); but unfortunately this server never seems to return an eof!

Any help appreciated! This has really been kicking my but!

Replies are listed 'Best First'.
Re: NET::SSH2 Terminates
by syphilis (Archbishop) on Nov 24, 2010 at 00:37 UTC
    Have you tried $chan2->blocking(1); ?

    With blocking turned off my (Windows) client often gets nothing back form a local (Linux) server - but with blocking turned on it all works reliably.

    With blocking turned off, it's an "all or nothing" situation for me. I don't get the "partial" returns that you do, so maybe it's an entirely different problem that confronts you (with an entirely different solution :-)

    Cheers,
    Rob
Re: NET::SSH2 Terminates
by zentara (Cardinal) on Nov 24, 2010 at 12:51 UTC
    There are some server and client side Interval settings for the timeout. See setting ssh timeouts or google for "ssh timeouts".

    If those settings don't do it, you might try the various methods available in perldoc Net::SSH2::Channel. For instance:

    Setting blocking to 1, or instead of $chan2->close; you might try $chan2->send_eof, or ask for a pty with $chan2->pty

    There is a problem with a Net::SSH2 connection compared to a c ssh utility connection, in that the c utility will automatically detach itself when you try to run a background process.... it shows up in my pid list designated as as no_tty ? But with Net::SSH2, you need to manually do it. Thats my incomplete understanding.

    There is some updated example code for exec'ing a program into the background at A little demo for Net::SSH2, and you may have to do that as a last resort.

    # to run a remote command in the background # you need to semi-daemonize it by redirecting it's filehandles my $chan3 = $ssh2->channel(); $chan3->blocking(1); $chan3->exec("nohup /home/zentara/perlplay/net/zzsleep > foo.out 2> fo +o.err < /dev/null &"); $chan3->send_eof;
    But for your particular problem, it sounds like you are closing $chan2, before the data can be completely transferred, and the randomness of the amount of return you get, is just system load variations playing out, while the $chan2->close is executed. Have you tried commenting out $chan2->close; and do a wait_closed or something similar, as described in perldoc Net::SSH2::Channel?

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: NET::SSH2 Terminates
by salva (Canon) on Nov 24, 2010 at 09:23 UTC
    It seems you are in a Unix/Linux box, you can try using Net::OpenSSH instead.