Hello All, I have the script below that is suppose to run against a file with a list of ip addresses, check if they are up or not .If they are up just print, if they are not write to a file after it determines if its empty or not, but I also want that file to have no duplicates, so I have the following, seems like over kill to do a simple task.It runs, but when I get to if the host is not up, that section doesn't even run (I know this because I put dead ip addresses in the list.Can you please assist

#!/usr/bin/perl use warnings; use strict; my $awsGone = "/ansible/awsGone"; my $awsLists = "/ansible/awsLists"; my @ips; open(FILE, '<', $awsLists) || die("Could not open file, $!"); @ips = <FILE>; close FILE; foreach my $addy(@ips) { chomp($addy); #Test if up my $retreval=system qq{nc -w 1 $addy 22 > /dev/null 2>&1}; if($retreval==0) { print "This up -> $addy\n"; } else { ##check if the file is empty, dont bother searching for +duplicates if(-z "$awsGone") { print "Host not up\n"; open(WRITE,'+>>',"$awsGone") || die("canot open $awsGone,$ +!"); print WRITE "$addy\n"; close WRITE; #remove dead ip from file system qq{perl -p -i -e "/$addy/" $awsLists}; } else { ##ip is dead, so open file for writing, then check if d +ead ip is already there open(WR,'+>>',"$awsGone") || die("canot open $awsGone,$!") +; while(my $line = <WR>) { if($line =~ /$addy/) { print "already exists\n"; } else { ##Append dead ip to the file print "Host not up now\n"; print WR "$addy\n"; close WR; system qq{perl -p -i -e "/$addy/" $awsLists}; } } } } }
My test file looks like below, all are good except the 3rd and 4th
ip-10-1-0-152.us-west-2.compute.internal ip-10-1-0-239.us-west-2.compute.internal ip-10-55-55-55.us-west-2.compute.internal ip-10-122-122-122.us-west-2.compute.internal ip-10-1-1-143.us-west-2.compute.internal ip-10-1-1-149.us-west-2.compute.internal ip-10-1-1-150.us-west-2.compute.internal ip-10-1-1-167.us-west-2.compute.internal
my results are
This up -> ip-10-1-0-152.us-west-2.compute.internal This up -> ip-10-1-0-239.us-west-2.compute.internal This up -> ip-10-1-1-143.us-west-2.compute.internal This up -> ip-10-1-1-149.us-west-2.compute.internal This up -> ip-10-1-1-150.us-west-2.compute.internal This up -> ip-10-1-1-167.us-west-2.compute.internal

In reply to Writing to File based on condition by cbtshare

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.