?– Note Several examples in this section use the system() function. This function is introduced in
Chapter 10.
Pinging a Server
Verifying a server??™s connectivity is a commonplace administration task. The following
example shows you how to do so using PHP:
// Which server to ping?
$server = "www.example.com";
// Ping the server how many times?
$count = 3;
CHAPTER 16 ?– NETWORKING 419
// Perform the task
echo "
";
system("/bin/ping -c $count $server");
echo "
";
// Kill the task
system("killall -q ping");
?>
The preceding code should be fairly straightforward except for perhaps the system call
to killall. This is necessary because the command executed by the system call will
continue to execute if the user ends the process prematurely. Because ending execution
of the script within the browser will not actually stop the process for execution
on the server, you need to do it manually.
Sample output follows:
PING www.example.com (192.0.34.166) from 123.456.7.8 : 56(84) bytes of data.
64 bytes from www.example.com (192.0.34.166): icmp_seq=0 ttl=255 time=158 usec
64 bytes from www.example.com (192.0.34.166): icmp_seq=1 ttl=255 time=57 usec
64 bytes from www.example.com (192.0.34.166): icmp_seq=2 ttl=255 time=58 usec
--- www.example.com ping statistics ---
5 packets transmitted, 3 packets received, 0% packet loss
round-trip min/avg/max/mdev = 0.
Pages:
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499