in reply to Executing independent commands in PERL

For information on starting up another process and letting it run independently of your own, check out perlipc.

But you may want to try and get rid of the external program altogether and use Net::Ping. Then you have control over the entire process.

An example from the docs:

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

Replies are listed 'Best First'.
Re: Re: Executing independet commands in PERL
by jink (Scribe) on Mar 05, 2001 at 04:25 UTC
    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'}}; }
      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.