in reply to Writing to File based on condition
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Writing to File based on condition
by cbtshare (Monk) on Feb 18, 2018 at 04:11 UTC | |
|
Re^2: Writing to File based on condition
by cbtshare (Monk) on Feb 18, 2018 at 03:57 UTC | |
by shmem (Chancellor) on Feb 18, 2018 at 16:17 UTC | |
by cbtshare (Monk) on Feb 18, 2018 at 18:05 UTC | |
by shmem (Chancellor) on Feb 18, 2018 at 18:44 UTC |