in reply to Re: Executing independet commands in PERL
in thread Executing independent commands in PERL

Net::Ping doesn't use ICMP, but UDP/TCP to connect to the echo port of a machine. I tried using it, but found out you couldn't use ICMP and most machines don't have their echo port enabled for obvious reasons... In my current version I use Open3. The options for ping probably don't apply to Windows ping. I use FreeBSD 4.2 here.
#!/usr/local/bin/perl5 -w use IPC::Open3; use Symbol; # .ping contains hostnames, each on a seperate line. open(HOSTS, "<.ping") || die("Can't open hostsfile! ($!)\n"); @hosts = <HOSTS>; close(HOSTS); %PING = (); %STATUS = ( -1 => "Host not found", 0 => "Not done", 1 => "Host is alive", 2 => "No reply from host", ); foreach $host (@hosts) { chomp $host; $WTR = gensym(); $RDR = gensym(); $ERR = gensym(); $PING{$host}{'PID'} = open3($WTR, $RDR, $ERR, "/sbin/ping -n -q -c 1 + $host"); close($WTR); while (<$RDR>) { $PING{$host}{'OUT'} .= $_; } while (<$ERR>) { $PING{$host}{'ERR'} .= $_; } $PING{$host}{'STATUS'} = 0; } foreach $host (keys %PING) { if(defined($PING{$host}{'ERR'})) { if($PING{$host}{'ERR'} =~ /Unknown host/) { $PING{$host}{'STATUS'} = -1; next; } } if($PING{$host}{'OUT'} =~ /.*, (.*) packets received.*/) { $PING{$host}{'STATUS'} = ($1 == 1?1:2); } } foreach $host (keys %PING) { printf "%-30s %s\n", $host, $STATUS{$PING{$host}{'STATUS'}}; }

Replies are listed 'Best First'.
Re: Re: Re: Executing independet commands in PERL
by btrott (Parson) on Mar 05, 2001 at 05:44 UTC
    According to the docs it supports icmp:
    $p = Net::Ping->new("icmp"); foreach $host (@host_array) { print "$host is "; print "NOT " unless $p->ping($host, 2); print "reachable.\n"; sleep(1); } $p->close();
      I see. But taken from the NOTES from these docs:
      The icmp protocol requires that the program be run as root or that it be setuid to root. The tcp and udp protocols do not require special privileges, but not all network devices implement the echo protocol for tcp or udp.
      I'd rather not use setuid root.