Error Handling in Net::SSH::Perl

  • Follow


Hi, I'm a bit naive with both Perl and SSH, but I'm trying to write a
script that checks a bunch of Cisco devices to verify that it can log
into them via SSH.  I installed Net::SSH::Perl, and it is working well.

However, the source code of Net::SSH::Perl makes several calls to die()
or croak() when it has any problems connecting such as not being able
to reach the host, bad password, etc.  Some of the functions that do
this are _connect and _setup_connection.  I don't want the program to
die when these things happen though; I just want my script to know that
it was unsuccessful (so it can log this into a database, for example)
and continue to run.

I've written my own versions of _connect and _setup_connection to
handle this; I just have the functions return -1 instead of croaking,
but my code is getting really ugly.  Does anyone have any suggestions
on how to handle this?  I'd appreciate the help.

0
Reply keithw99 (2) 11/13/2006 8:58:13 PM

Keith wrote:
> Hi, I'm a bit naive with both Perl and SSH, but I'm trying to write a
> script that checks a bunch of Cisco devices to verify that it can log
> into them via SSH.  I installed Net::SSH::Perl, and it is working well.
>
> However, the source code of Net::SSH::Perl makes several calls to die()
> or croak() when it has any problems connecting such as not being able
> to reach the host, bad password, etc.  Some of the functions that do
> this are _connect and _setup_connection.  I don't want the program to
> die when these things happen though; I just want my script to know that
> it was unsuccessful (so it can log this into a database, for example)
> and continue to run.
>
> I've written my own versions of _connect and _setup_connection to
> handle this; I just have the functions return -1 instead of croaking,
> but my code is getting really ugly.  Does anyone have any suggestions
> on how to handle this?  I'd appreciate the help.

Exception handling in Perl is most basically handled by eval().  Just
wrap your call to functions that may day in an eval{} block.  If the
functions called die() or croak(), your program will continue like
normal, but $@ will be set to the error message that would have been
printed to STDERR.

my $ssh;
eval {
   $ssh = Net::SSH::Perl->connect( ... );
};
if ($@) {
  warn "Connect failed: $@\n";
}

Paul Lalli

0
Reply Paul 11/13/2006 9:02:29 PM


Keith wrote:
> I don't want the program to
> die when these things happen though; I just want my script to know that
> it was unsuccessful (so it can log this into a database, for example)
> and continue to run.

Then wrap your call in an eval

   perldoc -f eval


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

0
Reply usenet 11/13/2006 9:06:30 PM

Awesome -- exactly what I was looking for.  Thanks everyone!

usenet@DavidFilmer.com wrote:
> Keith wrote:
> > I don't want the program to
> > die when these things happen though; I just want my script to know that
> > it was unsuccessful (so it can log this into a database, for example)
> > and continue to run.
>
> Then wrap your call in an eval
>
>    perldoc -f eval
>
>
> --
> The best way to get a good answer is to ask a good question.
> David Filmer (http://DavidFilmer.com)

0
Reply Keith 11/13/2006 9:18:15 PM

my $ssh;
eval {
   $ssh = Net::SSH::Perl->connect( ... );
};
if ($@) {
  warn "Connect failed: $@\n";
}

It does not work with me, should we remove the option -w on the shebang or remove use warnings / use strict?
0
Reply dagomakoa (2) 6/22/2012 7:25:32 PM

Quoth dagomakoa <dago_makoa@hotmail.fr>:
> my $ssh;
> eval {
>    $ssh = Net::SSH::Perl->connect( ... );
> };
> if ($@) {
>   warn "Connect failed: $@\n";
> }
> 
> It does not work with me, should we remove the option -w on the shebang
> or remove use warnings / use strict?

No, that will certainly not help.

What error messages do you get, or what is not working?

Ben

0
Reply ben6057 (863) 6/22/2012 9:32:06 PM

On 06/22/12 14:25, dagomakoa wrote:
> my $ssh;
> eval {
>     $ssh = Net::SSH::Perl->connect( ... );
> };
> if ($@) {
>    warn "Connect failed: $@\n";
> }
>
> It does not work with me, should we remove the option -w on the shebang or remove use warnings / use strict?
>
>

You generally don't call 'connect' when using Net::SSH::Perl.

When you instantiate the class, using new(), it will 'connect' or 'die'
if the socket connection fails.

use Net::SSH::Perl;
eval {
	my $ssh = Net::SSH::Perl->new(... );
	$ssh->login( ... );
	my ( $o, $e, $ev ) = $ssh->cmd( ... );
};

die "SSH Failed: $@" if $@;


perldoc Net::SSH::Perl
0
Reply glex_no-spam (67) 6/25/2012 4:02:05 PM

You generally don't call 'connect' when using Net::SSH::Perl.

When you instantiate the class, using new(), it will 'connect' or 'die'
if the socket connection fails.

yeah ! That's right

Ben > My problem is that I do express a wrong password but I do not get desired error message

use Net::SSH::Perl;
eval {
	my $ssh = Net::SSH::Perl->new("$host");
	$ssh->login("$user", "$pass");
	
};

my $error = "SSH Failed: $@" if $@;

It seems that eval does me sending anything actually $@ is NULL




0
Reply dagomakoa (2) 6/28/2012 5:41:35 AM

On 06/28/12 00:41, dagomakoa wrote:
> You generally don't call 'connect' when using Net::SSH::Perl.
>
> When you instantiate the class, using new(), it will 'connect' or 'die'
> if the socket connection fails.
>
> yeah ! That's right

Please learn how to quote.

>
> Ben>  My problem is that I do express a wrong password but I do not get desired error message
>
> use Net::SSH::Perl;
> eval {
> 	my $ssh = Net::SSH::Perl->new("$host");
> 	$ssh->login("$user", "$pass");
> 	
> };
>
> my $error = "SSH Failed: $@" if $@;
>
> It seems that eval does me sending anything actually $@ is NULL

'eval' works fine and the above code is fine.  Look at the source
for the login method, it's a setter, it doesn't actually do
anything other than set the username and password it will use
when sending the command, so it's not going to 'die'.

When you actually attempt to run something:

$ssh->cmd(...);

is where it might die. cmd() returns the output, error
mesage, and the exit status, which makes it pretty easy
to use.

Take a look at the documentation before going any further:

perldoc Net::SSH::Perl
0
Reply glex_no-spam (67) 6/28/2012 3:34:43 PM

On 2012-06-22 21:25, dagomakoa wrote:

> my $ssh;
> eval {
>     $ssh = Net::SSH::Perl->connect( ... );
> };
> if ($@) {
>    warn "Connect failed: $@\n";
> }
>
> It does not work with me, should we remove the option -w on the shebang or remove use warnings / use strict?

Don't base code flow on $@.
Write it like this:

my $ssh;
eval {
     $ssh= Net::SSH::Perl->connect( ... );
     1;  # success
}
or do {
     my $eval_error = $@ || 'Zombie Error';
     warn "Connect failed: ", $eval_error, "\n";
};


or compacter:

my $ssh;
eval { $ssh= Net::SSH::Perl->connect( ... ); 1 }
   or warn "Connect failed: ", ( $@ || 'Zombie Error' ), "\n";


-- 
Ruud
0
Reply usenet8509 (84) 7/21/2012 10:48:49 AM

Quoth "Dr.Ruud" <rvtol+usenet@xs4all.nl>:
> On 2012-06-22 21:25, dagomakoa wrote:
> 
> > my $ssh;
> > eval {
> >     $ssh = Net::SSH::Perl->connect( ... );
> > };
> > if ($@) {
> >    warn "Connect failed: $@\n";
> > }
> >
> > It does not work with me, should we remove the option -w on the
> shebang or remove use warnings / use strict?
> 
> Don't base code flow on $@.
> Write it like this:
> 
> my $ssh;
> eval {
>      $ssh= Net::SSH::Perl->connect( ... );
>      1;  # success
> }
> or do {
>      my $eval_error = $@ || 'Zombie Error';
>      warn "Connect failed: ", $eval_error, "\n";
> };

    use Try::Tiny;

    my $ssh = 
        try { Net::SSH::Perl->connect(...) }
        catch {
            warn "Connect failed: $_\n";
            undef;
        };

The final 'undef' is the value assigned to $ssh if the connection fails.

Ben

0
Reply ben6057 (863) 7/22/2012 1:31:59 AM

"Dr.Ruud" <rvtol+usenet@xs4all.nl> writes:
> On 2012-06-22 21:25, dagomakoa wrote:
>
>> my $ssh;
>> eval {
>>     $ssh = Net::SSH::Perl->connect( ... );
>> };
>> if ($@) {
>>    warn "Connect failed: $@\n";
>> }
>>
>> It does not work with me, should we remove the option -w on the shebang or remove use warnings / use strict?
>
> Don't base code flow on $@.
> Write it like this:
>
> my $ssh;
> eval {
>     $ssh= Net::SSH::Perl->connect( ... );
>     1;  # success
> }
> or do {
>     my $eval_error = $@ || 'Zombie Error';
>     warn "Connect failed: ", $eval_error, "\n";
> };

This should still be: Don't use exceptions. That's just .... wrong!
The problem with this is, not everyone whose code you might need to
use at some point in time cares for these semi-religious convictions.
Eg, the mod_perl error handling is exception based. Since $@ can also
contain a reference to some object, there might even be some kind of
structured error handling based on some hierarchy of exception
classes. Even real error handling, such as automatically reconnecting
to some database server and re-executing queued statements, possibly,
after re-preparing them, in case the error was not caused by a
statement not just "Fuck! It didn't work!!" error handling aka 'Zombie
error! It returned zero and I don't know why!'
0
Reply rweikusat (2678) 7/22/2012 8:56:53 AM

On 2012-07-22 03:31, Ben Morrow wrote:
> "Dr.Ruud":
>> dagomakoa:

>>> my $ssh;
>>> eval {
>>>      $ssh = Net::SSH::Perl->connect( ... );
>>> };
>>> if ($@) {
>>>     warn "Connect failed: $@\n";
>>> }
>>>
>>> It does not work with me, should we remove the option -w on the
>>> shebang or remove use warnings / use strict?
>>
>> Don't base code flow on $@.
>> Write it like this:
>>
>> my $ssh;
>> eval {
>>       $ssh= Net::SSH::Perl->connect( ... );
>>       1;  # success
>> }
>> or do {
>>       my $eval_error = $@ || 'Zombie Error';
>>       warn "Connect failed: ", $eval_error, "\n";
>> };
>
>      use Try::Tiny;
>
>      my $ssh =
>          try { Net::SSH::Perl->connect(...) }
>          catch {
>              warn "Connect failed: $_\n";
>              undef;
>          };
>
> The final 'undef' is the value assigned to $ssh if the connection fails.

Try::Tiny is certainly fine for many situations.
http://www.perladvent.org/2011/2011-12-17.html
So use it if you can.


It presents itself as "bare bones" but it can't be,
mainly because of the minimal exception support in Perl.
So it needs to bring its own set of rules and exceptions and overhead.

At least check its Caveats section
http://search.cpan.org/perldoc?Try::Tiny
Also see: http://wonkden.net/dcbpw_ttch.html

-- 
Ruud

0
Reply usenet8509 (84) 7/22/2012 10:02:07 AM

12 Replies
383 Views

(page loaded in 0.207 seconds)

Similiar Articles:


















7/24/2012 9:03:58 AM


Reply: