Thank you, I have the following which works :
#!/usr/bin/perl
use warnings;
use strict;
my $awsGone = "/ansible/awsGone";
my $awsLists = "/ansible/awsLists";
#open file for appending
open(APPEND,'>>',"$awsGone") || die("canot open $awsGone,$!");
#open AWSlIST file for reading
open(READ, '<', $awsLists) || die("Could not open file, $!");
#open AWSGONE file for reading
open(READGONE, '<', $awsGone) || die("Could not open file, $!");
my @gone = <READGONE>;
my @ips;
@ips = <READ>;
foreach my $addy(@ips)
{
chomp($addy);
#Test if up
my $retreval=system qq{nc -w 1 $addy 22 > /dev/null 2>&1};
##if filesd are good, leave them alone, but if bad put them in $awsGon
+e
if($retreval==0)
{
print "This up -> $addy\n";
}
else
{
#check if file is empty, because after ansible runs it
+ clears this file
if(-z "$awsGone")
{
print "Host not up\n";
print APPEND "$addy\n";
#remove the dead ip from awsLists
system qq{sed -i "/$addy/d" "$awsLists"};
}
else
{
##file is not empty so check if ip is already
+ in the file
my @match = grep{/$addy/}@gone ;
if (@match)
{
print "already exists\n";
system qq{sed -i "/$addy/d" "$awsLists"};
}
else
{
print "Host not up \n";
print APPEND "$addy\n";
system qq{sed -i "/$addy/d" "$awsLists"};
}
}
}
}
close APPEND;
close READ;
close READGONE;