Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP Ping

Posted on 2011-03-21
5
Medium Priority
?
415 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

886 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