0

I am running PHP 7.2 on Ubuntu 18.04, Apache2.


What I want is simple : check server status, show if online or offline.

Server not responds if it is under http.(Connection always have to be https)

Server's certification is self-signed.


I'll say it is https://servername:10010/

The codes I tried are those :

$host = 'https://servername';
if($socket =@ fsockopen($host, 10010, $errno, $errstr, 10)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
}

and

$host = 'ssl://servername';
if($socket =@ fsockopen($host, 10010, $errno, $errstr, 10)) {
echo 'online!';
fclose($socket);
} else {
echo 'offline.';
}

and

require_once 'HTTP/Request2.php';
$request = new HTTP_Request2('https://servername:10010/', HTTP_Request2::METHOD_GET);
$request->setConfig(array(
    'ssl_verify_peer'   => FALSE,
    'ssl_verify_host'   => FALSE
));
try {
    $response = $request->send();    
    if (200 == $response->getStatus()) {
  //echo $response->getBody();//Get content of page
var_dump($response->getStatus());
    } else {
echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .$response->getReasonPhrase();
    }
} catch (HTTP_Request2_Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

and

$data = "";
$timeout = "1";
$services = array();
$services[] = array("port" => "10010", "service" => "blabla", "ip" => "https://servername") ;
$data .= "";
foreach ($services  as $service) {
    if($service['ip']==""){
       $service['ip'] = "localhost";
    }
    $fp = @fsockopen($service['ip'], $service['port'], $errno, $errstr, $timeout);
    if (!$fp) {
        $data .= "Off";
      //fclose($fp);
    } else {
        $data .= "On";
    }
}
echo $data;

None of theese worked.

Second One's Error (I saw) was Connection timed out. (BTW server is up)

SJang
  • 15
  • 4
  • Have you tried `CURL` . Besides that SSL typically runs on port 443 not 10010, but with CURL you can use `curl_setopt($ch, CURLOPT_PORT, 10010);` see also https://stackoverflow.com/questions/4372710/php-curl-https – ArtisticPhoenix Feb 17 '19 at 08:00

1 Answers1

0

Have you tried plain old CURL:

Taken from this answer

PHP CURL & HTTPS

But modified for the PORT:

/**
 * Get a web file (HTML, XHTML, XML, image, etc.) from a URL.  Return an
 * array containing the HTTP server response header fields and content.
 */
function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_SSL_VERIFYPEER => false,     // Disabled SSL Cert checks for self singed certs.
        CURLOPT_PORT           => 10010     // Port --- added this
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

Not sure if it will work. But this CURLOPT_SSL_VERIFYPEER disables the verification on the certificate, which can be useful when testing against Self singed certs.

See this answer how to fix that (properly verify the cert):

Verifying SSL certificate failing with Kohana

I would have done it as a duplicate, but this question is not specific to CURL, this is just an option that wasn't mentioned as being attempted.

Hope it works for you!

UPDATE

Seeing as all your PHP attempts failed, I think it could be an issue with a firewall blocking that port..

As I said in the comments:

Are you trying this script from the same computer, or is it on a server, you could have a firewall issue on a server that blocks that port. Personally I don't think it's PHP.

ArtisticPhoenix
  • 21,464
  • 2
  • 24
  • 38