Gunnar Hjalmarsson wrote:
monnappa appaiah wrote:

This the error i'm getting

Net::SSH2::connect: failed to connect to 10.10.10.1:22: Unknown error at
connect.pl line 21

the line 21 is shown below

 $ssh2->connect($_) or warn "Unable to connect host $_\n" and next;

It's apparent that the fatal error happens inside Net::SSH2, and warn() and next() are not run. The standard solution to this problem is eval():

    eval { $ssh2->connect($_) };
    if ($@) {
        warn "Unable to connect host $_: $@" and next;
    }

That is the "old fashioned" way. You really need to use the return value of eval to make sure.

    eval { $ssh2->connect($_); 1 }
    or do {
        warn "Unable to connect host $_: $@" and next;
    };

or:

    if ( not eval { $ssh2->connect($_); 1 } ) {
        warn "Unable to connect host $_: $@" and next;
    };


or better:

    eval {
        $ssh2->connect($_) };
        1;  # success
    }
    or do {
        my $err = $@ || "[unknown error]";
        warn "Unable to connect host $_: $err" and next;
    };

--
Ruud

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to