rts1676 has asked for the wisdom of the Perl Monks concerning the following question:
I'm playing around with PERL, never used it before and find some of the example scripts I run across to be useful - while I'm gearing up to spare free cycles to learning how to code properly. However, I found one on this site that I'm trying to modify slightly: The following is code:
use strict; use Net::Ping; # Declare Var my ($host, $x, $res); my $output = ''; # Servers To Ping my @AFIS = qw( mnbcamp001 mnbcamp002 mnbcamp003 mnbcamp004 mnbcamp005 ); # Ping Hosts Defined Above sub ping_hosts { foreach $host (@AFIS) { my $p = Net::Ping->new("icmp"); my $res = $p->ping($host); $output .= "Unknown $host\n" unless defined $res; if (!$res) { $output .= "$host Host is not reachable!\n"; } else { $output .= "$host Host is reachable!\n" } } } ping_hosts; # Print Results to Terminal print $output;
The script itself works but what I'd like to have happen is two things: Since I have about 85 servers that will be pinged via this method, I'd like to supply it with a text file which has the server names and/or IPs in it and secondly I'd like to have it output the data into a text file, and, if it's not super hard to do email it.
That being said, I did manage to add the output to file element but it gave me an uninitialized value in concatenation error for $res
Added where seemed appropriate
my $outfile = "output.txt"; open (OUTFILE, ">> $outfile") || die "ERROR: opening $outfile\n"; chomp ($host); print OUTFILE "Results for $host:\n"; print OUTFILE "$results\n\n";
Now, I'm a total newb, I'm just digging into this little bits at a time and playing around with things. Please, take it easy on me, I've got literature coming in the mail soon but figured why not ask a question and see what happens in the meantime. I appreciate any help I can get!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Newbie - Playing Around
by marto (Cardinal) on Dec 29, 2015 at 15:12 UTC | |
|
Re: Newbie - Playing Around
by BillKSmith (Monsignor) on Dec 29, 2015 at 19:41 UTC | |
|
Re: Newbie - Playing Around
by 1nickt (Canon) on Dec 29, 2015 at 14:55 UTC | |
by rts1676 (Initiate) on Dec 29, 2015 at 15:29 UTC |