#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11146781 use warnings; use Path::Tiny; use Time::HiRes qw( time ); use IO::Socket; my $port = 3143; # to my https://rosettacode.org/wiki/Hunt_the_Wumpus#Perl my @ips = map "192.168.1.$_:$port", 10 .. 20; # FIXME for testing my $logfile = path( "/tmp/d.11146781,log" ); # FIXME filename my $timeoutin = 0.1; # FIXME short for testing $logfile->spew( localtime() . " begin\n" ); my $start = time; for my $ip ( @ips ) { if( my $pid = fork ) {} # parent elsif( defined $pid ) # child { my $status = IO::Socket::INET->new( # test for connection PeerAddr => $ip, Timeout => $timeoutin, ) ? 'ok' : $@ =~ s/.*: //r; $logfile->append( sprintf "%s %s in %.3fs\n", $ip, $status, time - $start ); exit; } else { die "$! on fork" } # failure } 1 while wait >= 0; $logfile->append( localtime() . " end\n" ); print $logfile->slurp; # FIXME for testing # rename here... #### Mon Sep 12 09:54:46 2022 begin 192.168.1.13:3143 ok in 0.003s 192.168.1.14:3143 timeout in 0.102s 192.168.1.12:3143 timeout in 0.102s 192.168.1.16:3143 timeout in 0.102s 192.168.1.19:3143 timeout in 0.102s 192.168.1.10:3143 timeout in 0.103s 192.168.1.15:3143 timeout in 0.103s 192.168.1.11:3143 timeout in 0.103s 192.168.1.20:3143 timeout in 0.103s 192.168.1.18:3143 timeout in 0.103s 192.168.1.17:3143 timeout in 0.104s Mon Sep 12 09:54:46 2022 end