#!/usr/bin/env perl use strict; use warnings; use Parallel::ForkManager; use MCE::Shared; use IO::Socket; my $in_file2 = 'rang.txt'; open( my $input, "<", $in_file2 ) or die $!; my $result = MCE::Shared->array; my $manager = Parallel::ForkManager->new(20); foreach my $ip (<$input>) { $manager->start and next; MCE::Shared->init(); # Optional # This is called automatically for threads, MCE, MCE::Hobo, # and MCE::Child. Calling MCE::Shared->init assigns a data # channel in a round-robin fashion to ForkManager workers. # Omitting init has the effect of 1 data channel versus 10. chomp($ip); my $host = IO::Socket::INET->new( PeerAddr => $ip, PeerPort => 80, proto => 'tcp', Timeout => 1 ); if ( defined $host ) { $result->push($ip); } $manager->finish; } $manager->wait_all_children; close($input); # move shared array to unshared array reference $result = $result->destroy({ unbless => 1 }); open( my $output, ">", "port.txt" ) or die $!; print {$output} "$_\n" for @{ $result }; close($output);