Home arrow PHP arrow Page 5 - Adding Methods to the Query Processor in PHP

Gluing the pieces together: listing the full source code of the "QueryProcessor" class - PHP

Over this second article in a series covering network programming in PHP, you will learn how to run popular Windows network monitoring utilities, such as the “ping,” “netstat” and “ipconfig” programs, by adding some new methods to the “QueryProcessor” class introduced in the previous article.

TABLE OF CONTENTS:
  1. Adding Methods to the Query Processor in PHP
  2. Reviewing previous PHP networking functions: a quick look at the "QueryProcessor" class
  3. Adding more functionality to the "QueryProcessor" class: defining the "Ping()" and "IpConfig()" methods
  4. More methods ahead: defining the "Netstat()" and "getMXRecordsWin()" methods
  5. Gluing the pieces together: listing the full source code of the "QueryProcessor" class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 8
May 24, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Undoubtedly, you'll have a better idea of how the new methods fit into the structure of the "QueryProcessor" class if you see its full source code, so here is the complete definition of this class:

class QueryProcessor{
    private $host;
    private $services=array
('http','https','ftp','telnet','imap','smtp','nicname',
'gopher','finger','pop3','www');
    private $ports=array(21,23,25,43,70,79,80,110,143,443);
    public function __construct($host='myhost.com'){
        $this->host=$host;
    }
    // get IP address
    public function getIp(){
        if(!$ip=gethostbyname($this->host)){
            throw new Exception('Error resolving host IP
address.');
        }
        return $ip;
    }
    // get list of IP addresses
    public function getIpList(){
        if(!$ips=implode(' - ',gethostbynamel($this->host))){
            throw new Exception('Error getting list of IP
addresses for the provided hostname.');
        }
        return $ips;
    }
    // get host name
    public function getHost(){
        if(!$host=gethostbyaddr($this->getIp())){
            throw new Exception('Error resolving host name.');
        }
        return $host;
    }
    // get TCP ports of Internet services
    public function getServicePorts(){
        $output='Retrieving services ports...Please wait.<br />';
        foreach($this->services as $service){
            if(!$port=getservbyname($service,'tcp')){
                $output.='Error retrieving port of service
'.$service.'<br />';
            }
            else{
                $output.='Service '.$service. ' runs on TCP
port :'. $port.'<br />';
            }
        }
        return $output;
    }
    // get Services by TCP ports
    public function getServiceNames(){
        $output='Retrieving services names...Please wait.<br />';
        foreach($this->ports as $port){
            if(!$service=getservbyport($port,'tcp')){
                $output.='Error retrieving service name on port
'.$port.'<br />';
            }
            else{
                $output.='TCP Port '.$port. ' is used by
service :'. $service.'<br />';
            }
        }
        return $output;
    }
    // execute 'ipconfig' command on Windows systems
    public function IpConfig(){
        $output='Running ipconfig command...Please wait.<br />';
        exec('ipconfig',$lines);
        foreach($lines as $line){
            $output.=$line.'<br />';
        }
        return $output;
    }
    // execute 'ping' command on Windows systems
    public function Ping(){
        $output='Running ping command...Please wait.<br />';
        exec('ping '.$this->host,$lines);
        foreach($lines as $line){
            $output.=$line.'<br />';
        }
        return $output;
    }
    // execute 'netstat' command on Windows systems
    public function Netstat(){
        $output='Running netstat command...Please wait.<br />';
        exec('netstat',$lines);
        foreach($lines as $line){
            $output.=$line.'<br />';
        }
        return $output;
    }
    // get MX records on Windows systems
    public function getMXRecordsWin(){
        $output='Retrieving MX Records...please wait.<br />';
        exec("nslookup -type=mx $this->host",$mxhosts);
        foreach($mxhosts as $mxhost){
            $output.=$mxhost.'<br />';
        }
        return $output;
    }
}

Now that you've seen the complete source code of the "QueryProcessor" class, below I coded an example that demonstrates how to use some of these new methods:

// instantiate 'QueryProcessor' object
$queryProc=new QueryProcessor('google.com');
// display MX records
echo $queryProc->getMXRecordsWin();
/* displays the following output:

Retrieving MX Records...please wait.
Server: dns0c.dnserver.com.ar
Address: 200.51.212.7

google.com MX preference = 10, mail exchanger = smtp1.google.com
google.com MX preference = 10, mail exchanger = smtp2.google.com
google.com MX preference = 10, mail exchanger = smtp3.google.com
google.com MX preference = 10, mail exchanger = smtp4.google.com

google.com nameserver = ns3.google.com
google.com nameserver = ns4.google.com
google.com nameserver = ns1.google.com
google.com nameserver = ns2.google.com
smtp1.google.com internet address = 216.239.57.25
smtp2.google.com internet address = 64.233.167.25
smtp3.google.com internet address = 64.233.183.25
smtp4.google.com internet address = 66.102.9.25
ns1.google.com internet address = 216.239.32.10
ns2.google.com internet address = 216.239.34.10
ns3.google.com internet address = 216.239.36.10
ns4.google.com internet address = 216.239.38.10

*/

// display 'netstat' results
echo $queryProc->NetStat();
/* displays the following output:

Running netstat command...Please wait.

 Active Connections

Proto local address remote address  status
TCP mywebserver:http localhost:3677 ESTABLISHED
TCP mywebserver:3677 localhost:http ESTABLISHED

*/

// display 'ping' results
echo $queryProc->Ping();
/* displays the following output:
pinging google.com [72.14.207.99] with 32 bytes of data:

Response from 72.14.207.99: bytes=32 time=247ms TTL=237
Response from 72.14.207.99: bytes=32 time=234ms TTL=237
Response from 72.14.207.99: bytes=32 time=234ms TTL=237
Response from 72.14.207.99: bytes=32 time=234ms TTL=238

Ping statistics for 72.14.207.99:
Packets: sent = 4, received = 4, lost = 0
(0% lost),
Approximated response time en milliseconds:
Min = 234ms, Max = 247ms, Average = 237ms

*/

To wrap up

Over this second article in the series, you learned how to run popular Windows network monitoring utilities, such the "ping", "netstat" and "ipconfig" programs, by adding some new methods to the "QueryProcessor" class. Additionally, you saw how to retrieve the list of MX records corresponding to a given Internet host, by using the "exec()" PHP function, which comes in handy for running system commands.

Nevertheless, my introduction to PHP programming has not finished yet. In the last tutorial, I'll be defining a few additional methods for the "QueryProcessor" class that can be useful for scanning TCP ports, finding DNS records and much more. Just don't miss it!



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: