foreach $line ( @info ) { next if ( $line =~ /^#/ or $line eq "" ); ($ip, $host, $user, $passwd, $suser, $spasswd ) = split (/:/, $line ); print " INFO ==> $ip , $host, $user, $passwd, $suser, $spasswd \n\n"; ### - Creation of SSH object here itself -- ### my $ssh = Net::SSH::Expect->new(host=>$ip, user=>$user, password=>$passwd, raw_pty=>1 ) or die " Cannot Open "; $login_check=$ssh->login(); my $hs = $ssh->exec('hostname'); print " HOST of $host --> $hs \n"; #&nsecon($ip, $user, $passwd, $suser, $spasswd); ## &nsecon( $ssh ); push @threads, threads->create(\&nsecon, $ssh ); } while( my $thread = shift @threads ) { $thread->join(); print " Thread $counter exiting \n"; $counter++; } sub nsecon() { #($ip, $user, $passwd, $suser, $spasswd) = ( shift, shift,shift,shift,shift); $ssh = shift; #print "In Thread => $ip, $user, $passwd, $suser, $spasswd \n"; ### -- Commented this part as threads were exiting -- ### ### So created the ssh object in the main thread and passing it to the function ### ### my $ssh = Net::SSH::Expect->new(host=>$ip, #user=>$user, #password=>$passwd, #raw_pty=>1 ) or die " Cannot Open "; ### ) or die " Cannot Open "; #$login_check=$ssh->login(); print "LOGIN for $ip -- $login_check \n"; my $who = $ssh->send('ls'); #print "\n OUT1 $who \n"; while ( defined ($line = $ssh->read_line()) ) { print " Line => $line " . "\n"; } my $ls = $ssh->send('who am i '); #print "\n OUT2 - $ls \n"; while ( defined ($line = $ssh->read_line()) ) { print " Line => $line " . "\n"; } }