Adding Methods to the Query Processor in PHP - Gluing the pieces together: listing the full source code of the "QueryProcessor" class (
Page 5 of 5 )
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!