use IO::Socket; my $in_file2 = 'rang.txt'; open DAT,$in_file2; my @ip=; close DAT; chomp(@ip); foreach my $ip(@ip) { $host = IO::Socket::INET->new( PeerAddr => $ip, PeerPort => 80, proto => 'tcp', Timeout=> 1 ) and open(OUT, ">>port.txt"); print OUT $ip."\n"; close(OUT); }