#!/usr/bin/perl use strict; use warnings; $|++; use Carp; my $info = $ARGV[0] || 'string_of_data'; print outer($info); sub outer { use IO::Socket; my @ips = ('xxx.xxx.xxx.xx0','xxx.xxx.xxx.xx1','xxx.xxx.xxx.xx2'); my $port = xxxx; ### $SIG{INT} = \&buh_bye; ### per ikegami ### $SIG{ALRM} = \&fallback; ### per ikegami local $SIG{INT} = \&buh_bye; local $SIG{ALRM} = \&fallback; ### srand; ### woops, thanks again ikegami my $index = int(rand(scalar @ips)); alarm(5); my $sock = IO::Socket::INET->new($ips[$index].':'.$port); $index = fallback() unless ($sock); alarm(0); die "$! ($ips[$index])\n" unless ($sock); print $sock "$info$/"; sysread $sock, my ($text), 10000; close($sock); print "ok ($ips[$index])\n"; return $text; } sub fallback { my $signame = shift; print +($signame) ? "Caught SIG$signame! " : "$! "; print "($ips[$index])\nTrying fallback host "; $index -= 1; print "($ips[$index])\n"; $sock = IO::Socket::INET->new($ips[$index].':'.$port); return $index; } sub buh_bye { my $signame = shift; die "\nCaught SIG${signame}!\nIP: $ips[$index]\n"; }