Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: Writing to File based on condition

by cbtshare (Monk)
on Feb 18, 2018 at 04:11 UTC ( [id://1209411]=note: print w/replies, xml ) Need Help??


in reply to Re: Writing to File based on condition
in thread Writing to File based on condition

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209411]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (10)
As of 2024-04-23 08:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found