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

Hi Monks,

I was trying a code for executing commands on remote system using Net::SSH:Expect module. I have no problem in working with the code without multithreading. But when I appended threads with the existing code for better performance, it was giving errors.

Here is the code:

#!/usr/bin/perl use Net::SSH::Expect; use threads; @hosts = ('192.168.1.222',192.168.1.169); my $machine; foreach $machine ( @hosts ) { push @threads, threads->create(\&sshd,$machine); } while ( my $thread = shift @threads ) { $thread->join ; print " Thread [ $machine ] Ending ... \n"; } sub sshd { my $ssh = Net::SSH::Expect->new(host=>'$ip', user=>'root',pass +word=>'access') or die "cannot open"; $login_check=$ssh->login(); print "LOGIN for $ip -- $login_check \n"; my $who = $ssh->exec('hostname'); my $ls = $ssh->exec('ls -l |grep Desktop'); print "$who \n"; print "$ls \n"; $ssh->close(); }
Error:

ssh: : Name or service not known

thread failed to start: SSHConnectionAborted at sshnew.pl line 30

Thread Ending ...

please tell me any possible solution

Thanks in Advance.

Replies are listed 'Best First'.
Re: Problem With Net::SSH::Expect using Multi threads
by Joost (Canon) on Aug 20, 2007 at 13:32 UTC
      Hey I am compeled to use threads. Any other solution?
Re: Problem With Net::SSH::Expect using Multi threads
by Anonymous Monk on Aug 20, 2007 at 13:02 UTC
    try
    #!/usr/bin/perl use threads; @hosts = ('192.168.1.222',192.168.1.169); my $machine; foreach $machine ( @hosts ) { push @threads, threads->create(\&sshd,$machine); } while ( my $thread = shift @threads ) { $thread->join ; print " Thread [ $machine ] Ending ... \n"; } sub sshd { require Net::SSH::Expect; my $ssh = Net::SSH::Expect->new(host=>'$ip', user=>'root',pass +word=>'access') or die "cannot open"; $login_check=$ssh->login(); print "LOGIN for $ip -- $login_check \n"; my $who = $ssh->exec('hostname'); my $ls = $ssh->exec('ls -l |grep Desktop'); print "$who \n"; print "$ls \n"; $ssh->close(); }

      We have tried with replacing

      require Net::SSH::Expect;
      in the sub-routine but no luck