如何使用php检查端口2082和2222以检测服务器';s控制面板

如何使用php检查端口2082和2222以检测服务器';s控制面板,php,Php,如何使用php检查端口2082、2222以检测服务器的控制面板 $ports = array(2082 => 'cpanel', 2222 => 'directadmin' , ....); $panel = ""; foreach($ports as $port => $panelName){ // some codes for checking the port's // if(something) $panel = $panelName;

如何使用php检查端口2082、2222以检测服务器的控制面板

$ports = array(2082 => 'cpanel', 2222 => 'directadmin' , ....);
$panel = "";

foreach($ports as $port => $panelName){
   // some codes for checking the port's
   // if(something)
         $panel = $panelName;
}

这方面没有直接的解决方案,您需要实现自己的创造性方法。您可能需要检测一个打开套接字的控制面板,而另一个使用特定文件存在(例如,Cpanel将api文件放在php包含路径上)或任何其他想法

您可以为此目的使用该类
namespace Model;

// usage:
//include 'detectControlPanel';
//$object = new \model\detectControlPanel();
//$result = $object->getDetect();
//var_dump($result);

class detectControlPanel
{
public $debug = false;
protected $detect = array();

public function getDetect()
{
    return $this->detect;
}

public function __construct($debug = false)
{
    $this->run();
    $this->debug = $debug;
    $this->debug($this->detect);
}

public function run()
{
    $this->detect['cpanel'] = $this->isCpanel();
    $this->detect['virtualmin'] = $this->isVirtualmin();
    $this->detect['plesk'] = $this->isPlesk();
    $this->detect['directadmin'] = $this->isDirectadmin();
    return $this->detect;
}

private function isCpanel()
{
    try {
        $this->telnet('localhost', 2082);
        return true;
    } catch (\Exception $ex) {
        $this->debug($ex);
        return false;
    }
}

private function isVirtualmin()
{
    try {
        $this->telnet('localhost', 10000);
        return true;
    } catch (\Exception $ex) {
        $this->debug($ex);
        return false;
    }
}

private function isDirectadmin()
{
    try {
        $this->telnet('localhost', 2222);
        return true;
    } catch (\Exception $ex) {
        $this->debug($ex);
        return false;
    }
}

private function isPlesk()
{
    try {
        $this->telnet('localhost', 8443);
        return true;
    } catch (\Exception $ex) {
        $this->debug($ex);
        return false;
    }
}

private function debug($input)
{
    if ($this->debug == true) {
        if (gettype($input) == 'string') {
            echo '<br>' . "\n";
            echo $input;
            echo '<br>' . "\n";
        } elseif (gettype($input) == 'array') {
            echo '<pre>' . "\n";
            print_r($input);
            echo '</pre>' . "\n";
        } elseif (gettype($input) == 'object' && get_class($input) == 'Exception') {
            echo '<pre>' . "\n";
            echo $input->getMessage();
            echo '</pre>' . "\n";
        } else {
            var_dump($input);
        }
    }
}

private function telnet($hostname, $port)
{
    if (!$hostname)
        throw new \Exception("empty host name");
    if (!$port)
        throw new \Exception("empty port number");
    $ipAddress = gethostbyname($hostname);
    $link = @fsockopen($ipAddress, $port, $errno, $error);
    if ($error) {
        throw new \Exception($error, $errno);
    }
    if ($link) {
        return true;
    }
    return false;
}
}
名称空间模型;
//用法:
//包括“检测控制面板”;
//$object=new\model\detectControl面板();
//$result=$object->getDetect();
//var_dump($结果);
类检测控制面板
{
public$debug=false;
受保护的$detect=array();
公共函数getDetect()
{
返回$this->detect;
}
公共函数构造($debug=false)
{
$this->run();
$this->debug=$debug;
$this->debug($this->detect);
}
公共功能运行()
{
$this->detect['cpanel']=$this->isCpanel();
$this->detect['virtualmin']=$this->isVirtualmin();
$this->detect['plesk']=$this->isPlesk();
$this->detect['directadmin']=$this->isDirectadmin();
返回$this->detect;
}
专用功能面板()
{
试一试{
$this->telnet('localhost',2082);
返回true;
}捕获(\Exception$ex){
$this->debug($ex);
返回false;
}
}
私有函数isVirtualmin()
{
试一试{
$this->telnet('localhost',10000);
返回true;
}捕获(\Exception$ex){
$this->debug($ex);
返回false;
}
}
私有函数isDirectadmin()
{
试一试{
$this->telnet('localhost',2222);
返回true;
}捕获(\Exception$ex){
$this->debug($ex);
返回false;
}
}
私有函数isPlesk()
{
试一试{
$this->telnet('localhost',8443);
返回true;
}捕获(\Exception$ex){
$this->debug($ex);
返回false;
}
}
专用函数调试($input)
{
如果($this->debug==true){
if(gettype($input)='string'){
回显“
”。“\n”; echo$输入; 回显“
”。“\n”; }elseif(gettype($input)=='array'){ 回显“”。“\n”; 打印(输入); 回显“”。“\n”; }elseif(gettype($input)='object'&&get_类($input)=='Exception'){ 回显“”。“\n”; echo$input->getMessage(); 回显“”。“\n”; }否则{ 变量转储(输入); } } } 专用函数telnet($hostname,$port) { 如果(!$hostname) 抛出新\异常(“空主机名”); 如果(!$port) 抛出新\异常(“空端口号”); $ipAddress=gethostbyname($hostname); $link=@fsockopen($ipAddress,$port,$errno,$error); 如果($error){ 抛出新的\异常($error,$errno); } 如果($link){ 返回true; } 返回false; } }
也许你可以从这个答案中得到一些启示:可能重复