#!/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 () { 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"; } }