Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: test version of ssh from windows

by stevieb (Canon)
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1138178]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2022-08-18 11:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?