Solved

PHP Ping

Posted on 2011-03-21
5
406 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sort Multidimensional Array 11 34
Form Processing in PHP 11 30
using php variable inside javascript 5 11
Scope of $_SESSION 17 25
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

919 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

18 Experts available now in Live!

Get 1:1 Help Now