Skip to content

Instantly share code, notes, and snippets.

@saillinux
Created January 20, 2011 08:30
Show Gist options
  • Save saillinux/787581 to your computer and use it in GitHub Desktop.
Save saillinux/787581 to your computer and use it in GitHub Desktop.
anyevent non-blocking port check template
#!/usr/bin/env perl
use strict;
use warnings;
use AnyEvent::Socket;
use constant INTERVAL => 1;
my ($host, $port) = @ARGV;
die "No host name is given" unless defined $host;
die "No port numb is given" unless defined $port;
my $cv = AnyEvent->condvar;
my $once_per_second;
$once_per_second = AnyEvent->timer(interval => INTERVAL,
cb => \&port_check,
);
$cv->recv;
sub port_check {
tcp_connect $host, $port, sub {
my ($fh) = @_ or do {
die "The host or service is down\n"; # print 'timeout' at most every second
};
warn "Host is alive";
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment