Hey Monks,
Another thing where I have got stuck. Please advice me atleast in this case and need you sincere suggestion to move ahead and perform the tasks.
As you must have seen the above logic to login using normal user and switch to different user and perform operations.
I need to enhance this more by adding threads to the existing code, when did the same I am facing problems. I feel Net::SSH::Expect module is not supporting the threads facility, may because as we use exec()/send() function.
foreach $line ( @info )
{
next if ( $line =~ /^#/ or $line eq "" );
($ip, $host, $user, $passwd, $suser, $spasswd ) = spli
+t (/:/, $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,shif
+t,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"; }
}
When I am executing this script only the first thread returns the output and all the next threads are connecting to the systems and not returning nor the commands are getting executed.
Please help me out in this case.
Thanks In Advance.