Solved

PHP Ping

Posted on 2011-03-21
5
405 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
Comment Utility
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
Comment Utility
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
Comment Utility
<?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
Comment Utility
0
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now