Test if port open and forwarded using PHP

后端 未结 1 1449
半阙折子戏
半阙折子戏 2020-11-30 01:09

Full Disclosure: There\'s a similar question here.

Is there any way I can test if a particular port is open and forwarded properly using PHP? Specifically, how do I

1条回答
  •  渐次进展
    2020-11-30 02:02

    I'm not sure what you mean by being "forwarded properly", but hopefully this example will do the trick:

    $host = 'stackoverflow.com';
    $ports = array(21, 25, 80, 81, 110, 443, 3306);
    
    foreach ($ports as $port)
    {
        $connection = @fsockopen($host, $port);
    
        if (is_resource($connection))
        {
            echo '

    ' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.

    ' . "\n"; fclose($connection); } else { echo '

    ' . $host . ':' . $port . ' is not responding.

    ' . "\n"; } }

    Output:

    stackoverflow.com:21 is not responding.
    stackoverflow.com:25 is not responding.
    stackoverflow.com:80 (http) is open.
    stackoverflow.com:81 is not responding.
    stackoverflow.com:110 is not responding.
    stackoverflow.com:443 is not responding.
    stackoverflow.com:3306 is not responding.
    

    See http://www.iana.org/assignments/port-numbers for a complete list of port numbers.

    0 讨论(0)
提交回复
热议问题