11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

www.it-ebooks.infoCHAPTER 16 • NETWORKINGPinging a ServerVerifying a server’s connectivity is a commonplace administration task. The following example showsyou how to do so using <strong>PHP</strong>:// Which server to ping?$server = "www.example.com";// Ping the server how many times?$count = 3;// Perform the taskecho "";system("/bin/ping -c $count $server");echo "";// Kill the tasksystem("killall -q ping");The preceding code should be fairly straightforward except for perhaps the system call to killall. Thisis necessary because the command executed by the system call will continue to execute if the user endsthe process prematurely. Because ending execution of the script within the browser will not actually stopthe process for execution on the server, you need to do it manually.Sample output follows:PING www.example.com (192.0.32.10) 56(84) bytes of data.64 bytes from www.example.com (192.0.32.10): icmp_seq=1 ttl=243 time=84.0 ms64 bytes from www.example.com (192.0.32.10): icmp_seq=3 ttl=243 time=84.2 ms--- www.example.com ping statistics ---3 packets transmitted, 2 received, 33% packet loss, time 2009msrtt min/avg/max/mdev = 84.095/84.178/84.261/0.083 ms<strong>PHP</strong>’s program execution functions are great because they allow you to take advantage of anyprogram installed on the server that has the appropriate permissions assigned.Creating a Port ScannerThe introduction of fsockopen() earlier in this chapter is accompanied by a demonstration of how tocreate a port scanner. However, like many of the tasks introduced in this section, this can beaccomplished much more easily using one of <strong>PHP</strong>’s program execution functions. The followingexample uses <strong>PHP</strong>’s system() function and the Nmap (<strong>net</strong>work mapper) tool:345

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!