Skip to content

Instantly share code, notes, and snippets.

@lottspot
Created July 31, 2013 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lottspot/6123063 to your computer and use it in GitHub Desktop.
Save lottspot/6123063 to your computer and use it in GitHub Desktop.
Test connectivity to a websocket Original script: http://johnallen.us/?p=332
#!/usr/bin/perl
#
# nettest.pl
#
use IO::Socket;
use Errno;
$| = 1;
$host = shift;
$port = shift;
$type = shift || "tcp";
$test = shift || "X"; ## "C" for Continuous testing
die "No Host parameter!" if !$host;
die "No Port parameter!" if !$port;
while (true) {
my $rc = new IO::Socket::INET
PeerAddr => $host,
PeerPort => $port,
Proto => $type,
Timeout => 2;
if ($type eq "udp" or $type eq "UDP") { ## UDP Socket
my $buf;
$rc->send("--TEST LINE--") or die "Send Error: $!\n";
eval {
local $SIG{ALRM} = sub { die "-Timeout-\n" };
alarm 2;
$rc->recv($buf,1000);
alarm 0;
print "Success connecting to $host on port: $port\n";
};
if ($@ eq "-Timeout-\n") {
print "Cannot Connect: Timeout!\n";
}
} else { ## TCP Socket
if ($!{EISCONN}) { ## Connected!!
print "Success connecting to $host on port: $port\n";
} else {
if ($!{EINVAL}) { ## "Invalid Argument" => Connection Refused
print "Cannot Connect: CONNECTION REFUSED!\n";
}
if ($!{ENOTCONN} || $!{ETIMEDOUT}) { ## NotConnected or Timed Out, Error.
print "Cannot Connect: -$!\n";
}
if ($!{EALREADY} || $!{EINPROGRESS}) { ## InProgress or Already Started, try again.
close $rc;
my $rc = new IO::Socket::INET
PeerAddr => $host,
PeerPort => $port,
Proto => $type,
Timeout => 4;
if ($!{EISCONN}) {
print "Success connecting to $host on port: $port\n";
}
print "Cannot Connect to $host on port: $port\n"
} else {
## Unknown Error
print "Cannot Connect: {$!}\n";
}
}
}
close $rc;
if ($test ne "C") {
exit;
}
sleep 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment