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

    Welcome! Firstly reading your server list from a file:

    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $serverfile = 'servers.txt'; open (my $fh, '<', $serverfile) or die "can't open file $serverfile: $ +!"; chomp(my @servers = <$fh>); close $fh; print Dumper @servers;

    In the above example I use the Data::Dumper module to print the array, you obviously don't need this part. Also note the use of $! to tell you why trying to open the file failed. For further info take a look at open.

    For sending mail I use the MIME::Lite module, example code in the documentation.

    "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"

    Can you please post the exact error and the code which generates it? How do I post a question effectively? has tips on how to best achieve this around here.

Re: Newbie - Playing Around
by BillKSmith (Monsignor) on Dec 29, 2015 at 19:41 UTC
    You do not have to wait for literature. If you have perl, you already have a great deal of quality documentation which you can read with the tool perldoc. To learn to use it, type: perldoc perldoc
    Bill
Re: Newbie - Playing Around
by 1nickt (Canon) on Dec 29, 2015 at 14:55 UTC

    ... but figured why not ask a question ...

    So go ahead: ask a question.


    The way forward always starts with a minimal test.

      Indeed. Forgive my general lack of forum etiquette as I typically don't actively participate in them. When time allows I will post a better and more acceptable question in response here.