如何检测服务器的某个端口是否开放?
感觉最简单的就是在线扫描工具,比如使用站长工具:http://tool.chinaz.com/port/,直接输入IP和端口就可以了。
当然也可以下载第三方工具来扫描测试。
今天在网上闲逛,看到有人用PHP代码来扫描端口,
因本人比较爱好PHP,所以记录一下。
代码如下:
<?php $host = '47.52.130.14'; //要ping的地址,也可以是IP $port = '8080'; //要ping的端口 $num = 3; function microtime_float() { list($usec, $sec) = explode(" ", microtime()); return ((float)$usec + (float)$sec); } function ping($host,$port) { $time_start = microtime_float(); $ip = gethostbyname($host); $fp = @fsockopen($host,$port,$errno,$errstr,1); if(!$fp) return 'replay time out!'; $get = "GET / HTTP/1.1\r\nHost:".$host."\r\nConnection: Close\r\n\r\n"; @fputs($fp,$get); @fclose($fp); $time_end = microtime_float(); $time = $time_end - $time_start; $time = ceil($time * 1000); return 'Reply from '.$ip.': time='.$time.'ms<br />'; } echo 'Pinging '.$host.' ['.gethostbyname($host).'] with Port:'.$port.' of data:<br /><br />'."\r\n"; for($i = 0;$i < $num;$i++) { echo ping($host,$port); sleep(1); }
这个端口测试不要测本地端口啊。要不然就不准确了。。