Way too complicated. You have a list of hosts, check them for upness, and you want to gather those that are down and blend them into file with no duplicates, right?
Then just read the hosts from file, determine upness for each and record that in a hash keyed with the host. After that, read the down-file and add those addresses to the down hash if they are not up. Write the keys into a new file, last do a rename. Something like
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;
Maybe the entries in the host file should be validated to determine their existence, i.e. resolving the names.
In reply to Re: Writing to File based on condition
by shmem
in thread Writing to File based on condition
by cbtshare
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |