cbtshare has asked for the wisdom of the Perl Monks concerning the following question:
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
My test file looks like below, all are good except the 3rd and 4th#!/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 results areip-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
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Writing to File based on condition
by Laurent_R (Canon) on Feb 16, 2018 at 23:10 UTC | |
|
Re: Writing to File based on condition
by 1nickt (Canon) on Feb 16, 2018 at 23:37 UTC | |
by cbtshare (Monk) on Feb 17, 2018 at 04:34 UTC | |
|
Re: Writing to File based on condition
by shmem (Chancellor) on Feb 17, 2018 at 10:42 UTC | |
by cbtshare (Monk) on Feb 18, 2018 at 04:11 UTC | |
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 | |
|
Re: Writing to File based on condition
by Anonymous Monk on Feb 16, 2018 at 22:34 UTC |