测试端口是否已打开并使用PHP转发 [英] Test if port open and forwarded using PHP
问题描述
完全公开:存在类似的问题这里.
Full Disclosure: There's a similar question here.
有什么方法可以测试特定端口是否已打开并使用PHP正确转发?具体来说,我该如何使用套接字通过给定的端口连接到给定的用户?
Is there any way I can test if a particular port is open and forwarded properly using PHP? Specifically, how do I go about using a socket to connect to a given user with a given port?
此示例在 WhatsMyIP.org/ports 的自定义端口测试"部分中.
An Example of this is in the 'Custom Port Test' section of WhatsMyIP.org/ports.
推荐答案
我不确定正确转发"是什么意思,但希望这个例子能解决这个问题:
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 '<h2>' . $host . ':' . $port . ' ' . '(' . getservbyport($port, 'tcp') . ') is open.</h2>' . "\n";
fclose($connection);
}
else
{
echo '<h2>' . $host . ':' . $port . ' is not responding.</h2>' . "\n";
}
}
输出:
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.
请参见 http://www.iana.org/assignments/port-numbers 获取端口号的完整列表.
See http://www.iana.org/assignments/port-numbers for a complete list of port numbers.
这篇关于测试端口是否已打开并使用PHP转发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!