使用特定端口和自签名证书检查https服务器状态

时间:2019-02-17 07:08:20

标签: php

我正在Ubuntu 18.04,Apache2上运行PHP 7.2。


我想要的很简单:检查服务器状态,显示联机还是脱机。

如果服务器位于http下,服务器将不响应。(连接必须始终为https)

服务器的证书是自签名的。


我会说是https://servername:10010/

我尝试过的代码是:

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

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

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();
}

$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;

这些人都没有工作。

第二个错误(我看到)是连接超时。 (BTW服务器已启动)

1 个答案:

答案 0 :(得分:0)

您是否尝试过普通的旧CURL:

从此答案中摘录

PHP CURL & HTTPS

但针对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;
}

不确定是否可以使用。但是此CURLOPT_SSL_VERIFYPEER禁用了证书上的验证,这在针对自认证证书进行测试时很有用。

查看此答案如何解决(正确验证证书):

Verifying SSL certificate failing with Kohana

我会做一个重复的例子,但这不是CURL特有的问题,这只是一个未提及的选择。

希望它对您有用!

更新

看到您所有的PHP尝试都失败了,我认为防火墙可能会阻止该端口。

正如我在评论中所说的:

  

是在同一台计算机上尝试运行此脚本,还是在服务器上尝试运行此脚本,可能会在阻止该端口的服务器上出现防火墙问题。我个人认为它不是PHP。