at2marty has asked for the wisdom of the Perl Monks concerning the following question:

I am new to Perl and in the process of learning.

I wrote a simple script to check two networks to see if the other hosts on those networks are up. I am sure that there is a better and more elegant way to do this. I'm open to any suggestions and/or pointers.

Here is what I have so far that is working. It's running on a Linux system if that matters.

#!/usr/bin/perl -w use strict; my $hostfile = "/etc/hosts"; my $pattern0 = "192.168"; my $pattern1 = "172.16"; my $eth_card0 = "eth0"; my $eth_card1 = "eth1"; chomp(my $ipaddr0 = `ifconfig | awk '/$eth_card0/ {getline; print}' | +awk '{print \$2}' | cut -d: -f2`); chomp(my $ipaddr1 = `ifconfig | awk '/$eth_card1/ {getline; print}' | +awk '{print \$2}' | cut -d: -f2`); open INPUT, '<', "$hostfile" or die "Cannot open $hostfile $!"; while (<INPUT>) { chomp; if ((/$pattern0/) or (/$pattern1/)) { my @hosts = split(" ", $_); foreach my $hosts (@hosts) { if (( $hosts =~ $pattern0 ) && ( $hosts ne $ipaddr0 ) || ( $hosts =~ $pattern1 ) && ( $hosts ne $ipaddr1 )) { pingip($hosts); } } } } close INPUT; sub pingip { my $rhost = $_[0]; my $retval = ""; system("ping -c 1 $rhost > /dev/null"); if ( $? != 0 ) { print "$rhost is not up\n"; } else { print "$rhost is up\n"; } }

My specific questions are first and foremost, is there a better way to do this? Second, can some of you that know more about Perl than me point out some of the errors that I might have made?

This script isn't anything that is real important to me, I'm simply using it as a learning tool mainly on working with arrays, and eventually hashes.

Thank you in advance for your help

Replies are listed 'Best First'.
Re: How do I check the status of a remote network
by keszler (Priest) on Sep 14, 2011 at 23:39 UTC

    Net::Ping is a pure Perl ping solution; that avoids all the potential anomalies of executing system commands. It can be as simple as

    use Net::Ping; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close();

    It also gives you the opportunity to use tcp, udp, or icmp protocol, and many other settings.

      Thank you for your suggestion. I really like that idea and will update my code to use it.

Re: How do I check the status of a remote network
by toolic (Bishop) on Sep 14, 2011 at 23:23 UTC
    Since . is a regex metacharacter, you should escape it using quotemeta:
    my $pattern0 = qutoemeta '192.168'; my $pattern1 = quotemeta '172.16';

      Thank you for that pointer toolie. I just read about using quotemeta. I had never seen or heard of it before.

Re: How do I check the status of a remote network
by onelesd (Pilgrim) on Sep 14, 2011 at 23:37 UTC
    Your script looks good enough, but code-golfing can be fun and a good way to learn. You might want to discover the interfaces rather than hard-coding eth0 and eth1 (what if there is an eth2? or just eth0?).

      Very good suggestion. I may research how to go about doing so.

Re: How do I check the status of a remote network
by norbert.csongradi (Beadle) on Sep 15, 2011 at 07:12 UTC
    You might consider using Nagios or Zabbix for network monitoring (you can still create new plugins for them in Perl ;)
Re: How do I check the status of a remote network
by Anonymous Monk on Sep 15, 2011 at 13:35 UTC
    Definitely Nagios. Everybody has a network they have to monitor.