#!/usr/bin/perl use strict; use warnings; use AnyEvent; use AnyEvent::HTTP; use AnyEvent::Socket qw( tcp_connect ); use Socket; my @hosts = qw(host1 host2); my @ports = qw(80); my $cv = AnyEvent->condvar; foreach my $port (@ports) { foreach my $host (@hosts) { print "[*] $host:$port\n"; $cv->begin; check_http(1111, $host); } } $cv->recv; sub check_http { my ($port_number, $host) = @_; my $ret_val; $host = 'http://' . $host; http_get $host, persistent => 1, on_prepare => sub { my ($fh) = @_; setsockopt($fh, SOL_SOCKET, SO_REUSEADDR, 1) or warn "Couldn't set SO_REUSEADDR"; bind($fh, sockaddr_in($port_number, INADDR_ANY)) or print "bind: $!\n"; 15 }, sub { my ($body, $hdr) = @_; if ($hdr->{Status} =~ /^2/) { print "Seems OK! (:\n"; } else { print "ERROR, $hdr->{Status} $hdr->{Reason}\n"; } print "[X] $host:$port_number\n"; $cv->end; }; }