0

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • What kind of communication do you want to do with the remote hosts? If they run a webserver send an HTTP request. – Halcyon Apr 16 '14 at 16:47
  • [Any of these](http://stackoverflow.com/questions/1239068/ping-site-and-return-result-in-php) any good? – halfer Apr 16 '14 at 16:47

1 Answers1

3

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.

hd1
  • 33,938
  • 5
  • 80
  • 91
  • Thanks! This worked like a charm. I was able to conclude that my current hosting server can't access the IPs that I needed. I tried the same code in two different hostings (in different countries too). One was able to connect with no problems but the other (the one I'm working in) got a "connection refused". – Francisco Fernandez Garrido Apr 16 '14 at 17:36
  • @hd1 when $fp is not true, means it encounters a error. If true means it succeeded. So the response code should be opposite. Shouldn't it? http://php.net/manual/en/function.fsockopen.php – Khurshid Alam Sep 14 '16 at 07:02
  • Edit the answer, @KhurshidAlam – hd1 Sep 14 '16 at 09:21