Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

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'; }

-stevieb

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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2018-07-18 05:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (383 votes). Check out past polls.

    Notices?