Here's a possible solution using the exec function to execute the ping command and the sleep function to wait for a specific interval between pings:
function pingMultipleIPs(array $ips, int $interval = 2) {
$pids = [];
foreach ($ips as $ip) {
$pid = pcntl_fork();
if ($pid == -1) {
// error
die("Could not fork process");
} elseif ($pid == 0) {
// child process
while (true) {
exec("ping -c 1 $ip", $output, $status);
if ($status == 0) {
echo "$ip is up\n";
} else {
echo "$ip is down\n";
}
sleep($interval);
}
exit(0);
} else {
// parent process
$pids[] = $pid;
}
}
// wait for child processes to finish
foreach ($pids as $pid) {
pcntl_waitpid($pid, $status);
}
}
This function takes an array of IP addresses and an optional interval (in seconds) between pings. It forks a child process for each IP address and executes the ping command in an infinite loop. The parent process waits for all child processes to finish before returning.
Note that this solution uses the pcntl_fork function to create child processes, which may not be available on all systems. If you're running PHP on Windows, you may need to use a different approach.