Francisco Fernandez Garrido Francisco Fernandez Garrido - 3 months ago 12
PHP Question

Need to check IP reachability via PHP or SSH (can't use ping though)

I need to check if my hosting server can reach certain IPs that I need to work with.
I have a shared hosting plan and, although I have access to SSH, the PING function is not available.
I've tried several php scripts but to no avail. Most of them relied on executing PING via shell.

Is there any way I can check this without using PING? I tried using the CURL function (in PHP) but it doesn't seem to work with IPs.

Any help would be very much appreciated.

hd1 hd1
Answer

Try this, it connects to port 22, which is the reserved port for ssh:

<?php
//
// By: Spicer Matthews <spicer@cloudmanic.com>
// Company: Cloudmanic Labs, LLC
// Date: 5/19/2011
// Description: This is a client to the echo server. It will send 10 test commands, and echo the server response.
//                          Run it from the command line "php client.php".
//
set_time_limit(0); 
$address = '127.0.0.1';
$port = '22';

$fp = fsockopen($address, $port, $errno, $errstr, 300);
if(! $fp) 
{
     http_response_code(204);
} 
else 
{   
     http_response_code(404);
}
?>

The response codes may need to be changed, as necessary.

Comments