Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Net::SSH2 Connect - Does not return error

by msk_0984 (Friar)
on Oct 08, 2007 at 10:34 UTC ( [id://643438]=perlquestion: print w/replies, xml ) Need Help??

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

Respected Monks,

It made my task easier and fekt happy when I came to know about the new Net::SSH2 perl module.

I have implemented this modules into my CGI application which connects to remote system and performs some execution of commands or else editing and saving of files.

I have initially tested the module with a sample perl script and could capture all the errors which could be generated during the process.

Here is the part of the code where we are trying to connect. Where ip -> is the remote systems IP

my $ssh2 = Net::SSH2->new; die "can't connect" unless $ssh2->connect("ip");
For eq: When SSH service is down on the remote system the error occured is

Net::SSH2::connect: failed to connect to <ip>:22: Connection refused a +t test.pl at line 10

I felt the error can be captured and have used the same in my CGI application and when ever the SSH service is down for a remote system. I am not able to capture the error, but it is being obtained in the

/var/apache/logs/error_log file

Net::SSH2::connect: failed to connect to ip:22: Connection refused at +./scripts/ssh_subrtns.pl line 2644

Here is the part of the code where I am trying to connect in my CGI application.

my $ssh2 = Net::SSH2->new(); #return "Can not connect $rip " unless ($ssh2->connect($rip)); + ## or die "Can not Connect $rip "; if ( ! ($ssh2->connect($rip)) ) or die "Can not Connect $rip " +; { print qq{<font color="red"><b> Can't Connect to $rip +</b></font></body></html>} ; exit; }
I have used "return" but did not find any luck and tried out die also but unable to print this error on the CGI interface. I need to display this error message for more readability to the user.

Please help me out if I am doing any thing wrong over here. Need your suggestions if any

Thanks in advance.


Sushil Kumar

Replies are listed 'Best First'.
Re: Net::SSH2 Connect - Does not return error
by atemon (Chaplain) on Oct 08, 2007 at 11:32 UTC

    Net::SSH2 has a function "error" which returns the last error. In scalar context, it returns the error code and in list context, it returns long error messages also.

    my $ssh2 = Net::SSH2->new(); $ssh2->connect($rip) ( $code, $error_name, $error_string ) = $ssh2->error(); if ( $code ); # $code will be false (undef) if there is NO error. { print "$code: $error_name: $error_string \n"; #format as you wish :) exit(); } ... <rest of your code>

    Cheers !

    --VC



    There are three sides to any argument.....
    your side, my side and the right side.

Log In?
Username:
Password:

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

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

    No recent polls found