Cobra_Fast
Cobra_Fast

Reputation: 16061

Can PHP ping a remote system without ICMP and without external programs?

I want to check if another machine is generally responding with PHP.

Most approaches are to ping some service running on the target machine, but I want to check if the machine is online generally.

I've found http://birk-jensen.dk/2010/09/php-ping/ which supposedly sends an ICMP Ping package. The problem is, somehow one is required to be root to perform a socket_create(AF_INET, SOCK_RAW, 1). The workaround via posix_seteuid(0) doesn't work either, since elevated permissions are required for that too.

Any functions that would let me run the ping program are not available in my scenario either.

So how do I check if a server is online using php?

Upvotes: 5

Views: 2823

Answers (2)

WhyteWolf
WhyteWolf

Reputation: 456

for this you might want to try, this is highly unlikely to give meaningful data, but is something to grok over.

if you open a tcp socket to a random port and instead of timing out it closes immediately. then the machine is "up", however this does not mean much more then that. it could be in a kernel panic.

and a timeout does not mean that the machine is down. just that instead of rejecting the tcp handshake it dropped it.

you wont be able to get any kind of meaningful timing data with this, but will give more of an educated guess if the machine in question is on or off.

<?php
$sock = socket_create(AF_INET,SOCK_STREAM,SOL_TCP);
socket_connect ( $sock , string $address,22 ) === true) {
switch(socket_last_error($sock)) {
    case 110:
    case 112:
    case 113:
       echo 'Machine may be down';
       break;
    case 111:
       echo 'Machine may be up';
       break;
    case 115:
       echo 'Machine is up';
       break;
    default:
       echo 'Machine in unknown state.';
       break;
}

you already stated that you can't use the ping command so Net_Ping will not work as it is just a fancy interface to the ping command in the operating system

Upvotes: 1

tix3
tix3

Reputation: 1152

You can always use Net_Ping to ping from php http://pear.php.net/package/Net_Ping/redirected

Upvotes: 1

Related Questions