Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: test version of ssh from windows

by stevieb (Abbot)
on Aug 11, 2015 at 15:05 UTC ( #1138178=note: print w/replies, xml ) Need Help??

in reply to test version of ssh from windows

Wrapping the socket operations within an eval block will help fix both issues. First, if connection is refused, the error will be put into $@ for later processing if you desire. Second, the timeout effect I believe you're after can be achieved with a local alarm signal handler, and setting the number of seconds before the alarm is raised (this error will also be put into $@ if triggered)...

#!/usr/bin/perl use strict; use warnings; use IO::Socket::INET; eval { local $SIG{ALRM} = sub { die 'timeout'; }; alarm 10; my $socket = IO::Socket::INET->new( PeerHost => $ARGV[0], PeerPort => 22, Proto => 'tcp', ); die "$!\n" unless $socket; $socket->print("\n"); my $output = join '', $socket->getline(); print $output; }; # if eval set an error... handle it if ($@){ ... if $@ eq 'timeout'; ... if $@ eq 'Connection refused'; }


ps. Note that in the IO::Socket::INET documentation, all of the parameters start with an upper-case letter (you mistyped 'Proto' and 'Timeout'). I don't know whether that's operationally important or not in this case, but I wanted to point it out. Not using an API according to the documentation can lead to incorrect results.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1138178]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2018-03-22 14:30 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (276 votes). Check out past polls.