msk_0984 has asked for the wisdom of the Perl Monks concerning the following question:
Thanks for your replies which I have recieved from and have helped me a lot to solve my problems and could also learn a lot through this site and from the well experienced monks.
Right now I am facing a problem with the error management of NET::SSH2 module. When I give a correct command say "hostname" i get the perfect output, but by mistake I gave it as "hostnam" and executed the program. It did not print any error status some thing when using Net::Telnet it also showed me the error by returning it but here i could not see any error its just blank.
Actually I have used threading and tried to connect to multiple systems using NET:SSH2 and executing commands on the remote systems for which I have written a subroutine. But when I have given a wrong command by mistake it did not prompt any error message.
Here is the part of my script where i implemented NET::SSH2 .
Here I got the perfect output for "hostname" . but since I gave a wrong command I didnot get any error message displaying "Command not found".$filepath = "host.conf"; $counter=0; if(open(FILE,$filepath)) { $inputline=<FILE>; while($inputline ne "") { chop($inputline); if($inputline !~ /#/) { if( @array = split(/:/,$inputline) ) { #print @array; $host = $array[1]; $user = $array[2]; $pass = $array[3]; push @threads, threads->create(\&sshcon, $host +, $user, $pass); } #if2 } #if1 $inputline=<FILE>; } #while while( my $thread = shift @threads ) { $thread->join(); print " Thread $counter exiting \n"; $counter++; } } else { die " File doesn't exists " } sub sshcon { my($h,$u,$p)=(shift,shift,shift); print " Host Info : $h,$u,$p \n"; my $ssh2 = Net::SSH2->new(); #$ssh2->debug(1); $ssh2->connect($h) or die " Cannot connect to $h "; print "Connected to host $h \n"; die "can't authenticate" unless $ssh2->auth(username => $u,password=>$ +p); # ,interact=>1); #print "Username/Password is correct\n"; $chan2 = $ssh2->channel; #print " Channel opened successfully \n"; die "Can't create a shell on channel " unless $chan2->shell; die " Cannot set blocking field " unless $chan2->blocking(0); print $chan2 "hostname \n"; print "Error --> " , $chan2->error() , "\n" ; print "LINE : $_" while <$chan2>; print $chan2 "who \n"; print "LINE : $_" while <$chan2>; $ssh2->disconnect(); }
The NET::SSH2 module will it not catch up what ever error generated.
OUTPUT
Connected to host 192.168.1.92 Error --> 0 LINE : Sun Microsystems Inc. SunOS 5.9 Generic May 2002 LINE : indialab92 LINE : SunOS indialab92 5.9 Generic_118558-34 sun4u sparc SUNW,Sun-Bla +de-1000 Thread 0 exiting
Only the Hostname is being displayed and no information about the "who" command.
Thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Error management in NET::SSH2
by zentara (Cardinal) on Sep 14, 2007 at 14:48 UTC |