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

Hi Monks, This is Srinivas, a beginner in Perl.

I was trying to connect to remote system by using Net::SSH::Perl module . I have installed all the required modules for this module to work and tried my hand on writing a small script by embeding it in multi threads. Here is the code

#!/usr/bin/perl use Net::SSH::Perl; use threads; $filepath="/tmp/Perl/host.conf"; if(open(FILE, $filepath)) { $inputline=<FILE>; while($inputline ne "") { chomp($inputline); if($inputline !~ /#/) { if( @array = split(/:/,$inputline) ) { $host = $array[1]; $user = $array[2]; $pass = $array[3]; print "\n\n\n ------ CHECK1 $user $host $pass +\n\n\n\n\n "; push @threads, threads->create(\&method, $host +, $user, $pass); } #if2 } #if1 $inputline=<FILE>; } #while while ( my $thread = shift @threads ) { $thread->join ; print " Thread [ $host ] Ending ... \n"; } } else { die " File doesn't exists " } sub method { my($hst,$usr,$pas)=(shift,shift,shift); &thrds($hst, $usr, $pas); } sub thrds { my($h,$u,$p)=(shift,shift,shift); $ssh = Net::SSH::Perl->new($h,debug=>'true'); $ssh->login($u,$p); my($stdout, $stderr, $exit) = $ssh->cmd('hostname'); my($stdout, $stderr, $exit) = $ssh->cmd('ls -l'); print $stdout; $ssh->close(); }
When I execute the above code with only two threads, the CPU is going upto 100% and system is getting hanged. My ram size is appriximately 4GB. I have no problem with a single thread or when executed without threads.

Did I mis-configured any thing. Please help me.

Thanks in advance,

Srinivas.

Replies are listed 'Best First'.
Re: Problem with threads in Net::SSH:Perl
by zentara (Cardinal) on Aug 24, 2007 at 13:03 UTC
    Another shot-in-the-dark..... maybe Net::SSH::Perl is not thread-safe..... it's an antiquated module. Try Net::SSH2

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

      I could not tell from the docs if Net::SSH2 is thread-safe or not. It would be nice to have this kind of info in the META.yml file ;)

      Now regarding Net::SSH::Perl I would not consider it antiquated, and it has its use: you can look at it almost as a pure-perl implementation (modulo a maths lib that someday could be part of core perl). It is true that the maintainer himself says that the module is showing signs of age and is hard to maintain but mostly IMHO because implementations of SSH1 and SSH2 are shipped in the same module (and even use different libs!).

      Another aspect is that building libssh2 is not so easy, and till recently there was no package on cygwin for it. Checking now I can see one libssh2; will try later to build Net::SSH2.

      I am quite fond of pssh actually; it is easy to hack: for example if you want to do local env management and run a few remote commands automatically before opening up a remote interactive shell.

      cheers --stephan
Re: Problem with threads in Net::SSH:Perl
by SFLEX (Chaplain) on Aug 24, 2007 at 12:15 UTC
    Im going to take a shot in the dark.....
    In ur code at line 27 u have
    $inputline=<FILE>;
    inside the while(), is that right for ur code to use that?
    Also if you have any other idea why the script could be hanging you could use one of these to un-hang it inside the while().
    next if !$_;
    or
    last if !$_;