Page 1 of 1

Pinging with PHP

Posted: Thu Jan 20, 2005 2:28 pm
by kendall
Hello,

I want to be able to get a response from a mail server that i dont have access to to find out if the server is up or down or the availability of the mail service

i was attempting to use the below code to get a response from the server in order to figure out its timeouts

Code: Select all

$host='apex-solutions.com';
$timeout=2;
  
//Open the socket
$handle=fsockopen('ddp://'.$host, 25, $errno, $errstr, $timeout);
if (!$handle)
     echo "$errstr ($errno)<br>\r\n";
else {
   //Set read timeout
   stream_set_timeout($handle, $timeout);
   for($i=0;$i<3;$i++){
       //Time the responce
       list($usec, $sec) = explode(" ", microtime(true));
     $start=(float)$usec + (float)$sec;
      
       //send somthing
         $write=fwrite($handle,"echo this\n");
       if(!$write){
           echo "Error in writing to socked<br>\r\n";
           break;
       }
       echo 'Send packet to '.$host;
      
       //Try to read. the server will most likely respond with a "ICMP Destination Unreachable" and end the read. But that is a responce!
       fread($handle,1024);
      
       //Work out if we got a responce and time it           
       list($usec, $sec) = explode(" ", microtime(true));
       $laptime=((float)$usec + (float)$sec)-$start;
       if($laptime>$timeout)
           echo " : No reply<br>\r\n";
       else   
           echo " : Round trip = ".round($laptime,3)." s<br>\r\n";
   }
   fclose($handle);
}
however i get the following response
Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /home/virtual/site15/fst/var/www/html/ping.php on line 9

Warning: fsockopen(): unable to connect to ddp://apex-solutions.com:25 in /home/virtual/site15/fst/var/www/html/ping.php on line 9
Success (0)
i am also using the below command to ping the server

Code: Select all

// ping some server five times and store output in array $output

exec("tracert 66.227.2.83", $output,$status);


// format each line of output

if($status){
	echo 'status '.$status.'<br>';
	echo 'output '.$output[2].'<br>';
 while(list(,$val) = each($output)){	
	echo 'ok';
		echo	"<p>".$key.' - '.$val."</p>";
}
}else{
echo 'failed';

}
but i get a
status 127
output
y am i not getting an output

Kendall

Posted: Thu Jan 20, 2005 2:47 pm
by timvw
see that i'm not having a ddp:// in my hostname

Code: Select all

$host = 'apex-solutions.com';
$fp = fopen($host, 25, $no, $str, $to);
if you are talking with smtp, you might want to read the relevant rfc and talk some valid protocol to the server (EHLO/HELO etc...)

Pinging with PHP

Posted: Fri Jan 21, 2005 9:21 am
by kendall
tim,

if you are talking with smtp, you might want to read the relevant rfc and talk some valid protocol to the server (EHLO/HELO etc...)
Can you point me in a direction. to be honest i dont even no what i need to do

Kendall

Posted: Fri Jan 21, 2005 1:55 pm
by timvw
http://www.faqs.org/rfcs/rfc2821.html

in that document you can find all the commands (section 4.1) ... and the replies (section 4.2)

Code: Select all

<?php

// +---------------------------------------------------------------------------
// | smtptest.php
// |
// | Author: Tim Van Wassenhove <timvw@users.sourceforge.net>
// | Update: 2005-01-21 20:53
// |
// | Test if a SMTP server is answering.
// +---------------------------------------------------------------------------

function smtptest($host, $timeout = 10)
&#123;
    $fp = fsockopen($host, 25, $errno, $errstr, $timeout);

    if (!$fp)
    &#123;
        echo "$errstr ($errno)<br />\n";
    &#125;
    else
    &#123;
        fwrite($fp, "QUIT\r\n");
        while (!feof($fp))
        &#123;
            echo fgets($fp, 128);
        &#125;
        fclose($fp);
    &#125;
&#125;

smtptest('smtp.scarlet.be');
?>