Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Error management in NET::SSH2

by msk_0984 (Friar)
on Sep 14, 2007 at 14:26 UTC ( [id://639014]=perlquestion: print w/replies, xml ) Need Help??

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

Respected Monks,

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 .

$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(); }
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".

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


Sushil Kumar

Replies are listed 'Best First'.
Re: Error management in NET::SSH2
by zentara (Archbishop) on Sep 14, 2007 at 14:48 UTC
    Well after your who command, you don't check for error, like you do after hostname. But it may be another glitch related to threading? Maybe try "who 2>&1\n"; ?
    print $chan2 "who \n"; print "Error --> " , $chan2->error() , "\n" ; print "LINE : $_" while <$chan2>; $ssh2->disconnect();

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://639014]
Approved by almut
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found