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

In reply to Error management in NET::SSH2 by msk_0984

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.