my $hostfile = "/ansible/awsLists"; my $deadfile = "/ansible/awsGone"; my $newdeadfile = $deadfile.'-tmp'; my %down; open my $ifh, '<', $hostfile or die "Can't read '$hostfile: $!\n"; while(my $addy = <$ifh>) { chomp $addy; $down{$addy} = system qq{nc -w 1 $addy 22 > /dev/null 2>&1}; # 0 = up, not 0 = down } open $ifh, '<', $deadfile or die "Can't read '$deadfile: $!\n"; while(my $addy = <$ifh>) { chomp $addy; $down{$addy} = 1 unless ! $down{$addy}; delete $down{$addy} if exists $down{$addy} and ! $down{$addy}; } # only the down ones remain in %down now open my $ofh, '>', $newdeadfile or die "Can't write '$newdeadfile': $!\n"; print $ofh $down{$_},"\n" for sort keys %down; close $ofh; rename $newdeadfile, $deadfile;