in reply to Re^3: A question of fork efficiency
in thread A question of fork efficiency

I started testing this again and there are still some issues with it I'm trying to work out. Sometimes when a server does not exist in DNS I get no output back for that server name. For example if I run this against this array:

qw ( server1 server2 server3 server4 doesnotexist server18 )

I'll get the following output:

alive server1 alive server2 alive server3 alive server4 not alive server18

As you can see the server called 'doesnotexist' just doesn't return output at all.

Replies are listed 'Best First'.
Re^5: A question of fork efficiency
by tybalt89 (Monsignor) on Aug 07, 2019 at 15:20 UTC

    Interesting. I cannot replicate this on my system.

    I added code for handling the case when the IO::Socket fails, it will now give a message. I don't see (yet) any way now to not get output for each server:port.

    #!/usr/bin/perl # http://perlmonks.org/?node_id=1135109 # http://perlmonks.org/?node_id=11103970 use strict; use warnings; use IO::Socket; use IO::Select; use Time::HiRes qw( time ); my @ips = map "192.168.1.$_", 1..20; # your IPs here push @ips, 'doesnotexist.com', 'doesnotexist'; # test for non existent my $port = 22; # your port here my $max = 1000; # somewhat smaller than max "open files" my $timeout = 1; my %handles; my $sel = IO::Select->new; while( @ips or $sel->count ) { if( @ips and $sel->count < $max ) { my $ip = shift @ips; if( my $fh = IO::Socket::INET->new(PeerHost => $ip, PeerPort => $p +ort, Proto => 'tcp', Blocking => 0)) { $handles{$fh} = ["$ip:$port", $fh, time]; $sel->add($fh); } else { print "not exist $ip:$port\n"; } } elsif( @ips ? $sel->count >= $max : $sel->count ) { my @connects; for my $fh ( @connects = $sel->can_write($timeout) ) { print $fh->connected ? ' ' : 'not', " alive $handles{$fh}[0]\ +n"; $sel->remove($fh); delete $handles{$fh}; } if( not @connects ) { my $time = time - $timeout; for my $key ( keys %handles ) { if( $handles{$key}[2] < $time ) { print "not alive $handles{$key}[0]\n"; $sel->remove( $handles{$key}[1] ); delete $handles{$key}; } } } } }

      One issue I'm seeing is that any server that gets to this point of the code:

      if( not @connects )

      Causes a 1 to 2 second delay. Looks like these are servers in DNS but not pingable.

        That's the basic 'timeout' point, It should only get there if all active attempted connections have timed out (not completed a connection in the $timeout interval).

      This should work the way I want. I'll rewrite it a bit to replicate how the current function works. Thanks for your help :)