Solved

PHP Ping

Posted on 2011-03-21
5
408 Views
Last Modified: 2012-06-27
Good morning,
Ive been trying to get a Ping working on a server, just to test if a particular server is up and working.

Ive tried system, exec, and passthru but get the error messeage:-
Warning: EXEC functions have been disabled for security reasons.

I have managed to get the following code working on my web and mail servers:-
<?php
$fp = fsockopen ("87.105.128.62", 25, $errno, $errstr, 30);
if (!$fp) {
   echo "Failed";
} else {

   echo ('Pinged OK');
   fclose ($fp);
} 
?>

Open in new window


Is it possible to open a socket up to a server that doesnt have a web or mail server on? Prefebably a ping request, but I know Pings work differently to this my using ICMP instead of TCP or UDP.

Thank you
0
Comment
Question by:tonelm54
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35179230
try this

<?php

$output = shell_exec('ping www.google.com');
echo "<pre>";e
cho $output;?>

refer

http://php.bigresource.com/Track/php-wc8hpzxd/
0
 

Author Comment

by:tonelm54
ID: 35179243
Sorry, trying the shell_exec, I still get:-
Warning: EXEC functions have been disabled for security reasons in /home/vincienergies/7KEXM7KR/htdocs/testPing.php on line 2
0
 
LVL 11

Accepted Solution

by:
NurAzije earned 500 total points
ID: 35179247
<?php
// Checksum calculation function
function icmpChecksum($data)
{
if (strlen($data)%2)
$data .= "\x00";
 
$bit = unpack('n*', $data);
$sum = array_sum($bit);
 
while ($sum >> 16)
$sum = ($sum >> 16) + ($sum & 0xffff);
 
return pack('n*', ~$sum);
}
// Making the package
$type= "\x08";
$code= "\x00";
$checksum= "\x00\x00";
$identifier = "\x00\x00";
$seqNumber = "\x00\x00";
$data= "Scarface";
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
$checksum = icmpChecksum($package); // Calculate the checksum
$package = $type.$code.$checksum.$identifier.$seqNumber.$data;
// And off to the sockets
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_connect($socket, "www.google.com", null);
// If you're using below PHP 5, see the manual for the microtime_float
// function. Instead of just using the microtime() function.
$startTime = microtime(true);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
echo round(microtime(true) - $startTime, 4) .' seconds';
}
socket_close($socket);
?>

Open in new window

0
 
LVL 11

Expert Comment

by:NurAzije
ID: 35179251
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35179270
try this
$strPing= `ping -ns www.whatever.com 4 4`;
echo "<pre>";
echo $strPing;
echo "</pre>";

OR

<?

$host='CHANGETHISTOYOURHOST';
$strPing= `ping $host`;
$posstring = strpos($strPing, "Please", 1);
if ($posstring == 0)
{ echo "Server online!"; }
else
{ echo "Server offline!"; }

?>
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question