PHP检查远程服务器端口并计算连接数

时间:2017-05-24 12:07:49

标签: php sql-server

我已经制作了一个PHP来检查本地的开放端口并计算连接的连接数并打印它。它在本地很好用,但是一旦我想远程检查(我的网络服务器在差异服务器上),它就无法获得结果。远程服务器是服务器2012,端口80关闭。以下是代码。

    $handle = fopen("Includes/online.txt", "r");
    $contents = fread($handle, filesize("Includes/online.txt"));
    fclose($handle);
    $contents = explode("\n", $contents);
    //print_r($contents);
if (!$contents || (time() - $contents[1]) > $ocs) {
    $command = "netstat -p TCP -n";
    exec($command,$status);
    //$status = PsExecute($command);
    //print_r($status);
    $status = array_slice($status, 5);
    $connections = array();
    foreach ($status as $stat => $s) {
    $c = preg_split('/ /', $s, -1, PREG_SPLIT_NO_EMPTY);
    if ($c[3] == 'ESTABLISHED') {
        $ipp = preg_split('/:/', $c[1], -1, PREG_SPLIT_NO_EMPTY);
        $ripp = preg_split('/:/', $c[2], -1, PREG_SPLIT_NO_EMPTY);


            for ($i=0; $i < 6; $i++ ){

        if ($ipp[1] == $config['server'][$i]['port']) {
        $connections[] = array('type'=>$c[0],'ip'=>$ipp[0],'port'=>$ipp[1],'rip'=>$ripp[0],'rport'=>$ripp[1],'con'=>$c[3]);

        }
        }
    }

    }

    $con = (!count($connections) ? 0 : count($connections))  ."\n". time();
    if ($fp = fopen('Includes/online.txt', 'w')) {
        fwrite ($fp, $con);
    }
    $con = array('online' => count($connections));
} else {

    $con = array('online' => $contents[0]);
}


//print_r($status);

以下是连接信息。

$config['server'] = array(array('ip'=>'ipxxx', 
'port'=>10201),array('ip'=>'ipxxx', 
'port'=>10202),array('ip'=>'ipxxx', 
'port'=>10203),array('ip'=>'ipxxx', 
'port'=>10204),array('ip'=>'ipxxx', 
'port'=>10205),array('ip'=>'ipxxx', 'port'=>10208));
 $ocs = "20";//in seconds it checks

代码的目的是在差异服务器(服务器2012)上远程检查开放端口并计算总连接数并打印它。我未能远程做到这一点。我需要在SQL服务器端口旁边打开任何端口,以便PHP成功连接到服务器吗? MSSQL已连接,我没有遇到任何问题。

1 个答案:

答案 0 :(得分:0)

对我来说,目前还不清楚你要求的是什么。

要检查SQL-Server上的活动TCP / IP连接,您可以使用存储过程sp_who2或此查询:

select *  from sys.sysprocesses where net_library like 'TCP/IP%'

SQL-Server具有默认的TCP / IP端口1433.必须在服务器端启用TCP / IP协议。

相关问题