当前位置:首页 > PHP教程 > php高级应用 > 列表

基于PHP实现端口批量查询工具

发布:smiling 来源: PHP粉丝网  添加日期:2024-04-20 19:25:46 浏览: 评论:0 

这篇文章主要为大家详细介绍了如何基于PHP实现端口批量查询工具,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以了解一下。

查询速度较慢,建议输入20个以内端口,暂未整理端口服务信息所以暂时不显示。

  1. <?php 
  2. error_reporting(0); //打开报错:去行首双斜杠 
  3. header('Content-type:text/html;charset = utf-8'); //如果乱码删行首双斜杠 
  4. header('Cache-Control: no-cache'); 
  5. header("X-Accel-Buffering: no"); 
  6.    
  7. $t = "PHP端口批量查询工具"; 
  8. $x = "序号|端口|开启否|端口服务"; 
  9.    
  10. $xx= explode("|",$x); 
  11. $ip = (isset($_POST['ip'])) ? $_POST['ip'] : '96448.cn';  
  12. $prt= array(21,22,23,25,53,80,110,143,161,443,465,587,993,995,1433,1521,3306,3389,5432,8080); 
  13. $msg= array('FTP','SSH','Telnet','SMTP','DNS','HTTP','POP3','IMAP','SNMP','HTTPS','SMTPS','SMTP','IMAPS','POP3S','MSSQL','Oracle','MySQL','RDP','PostgreSQL','HTTP'); 
  14.    
  15. if($_GET["x"] == "cha"){ 
  16. $prt = isset($_POST['prt']) ? $_POST['prt'] : "80,443,22,21,3306,3389"; 
  17. $dms = isset($_POST['doma']) ? $_POST['doma'] : "12391.net"; 
  18. preg_match_all('/^\d+:\d+$/', $prt, $pts); 
  19. if($pts[0][0]){ 
  20. list($p1,$p2)=explode(":",$pts[0][0]); 
  21.  $pt=array(); $p3 =$p1+$p2; echo "<!-- |$p1|$p2|$p3| -->"; 
  22.  for($ii=$p1;$ii<$p3;$ii++){ $pt[]=$ii;} 
  23.     }else{ 
  24.  preg_match_all('/\d+/', $prt, $pts); $pt=$pts[0]; 
  25. } 
  26. echo "<!--".join(",",$pt)."-->"; 
  27.    foreach ($pt as $key => $value) { 
  28.      echo '<tr>'; 
  29.      echo '<td>' . $key . '</td>'; 
  30.      echo '<td>' . $value . '</td>'; 
  31.      $fp = @fsockopen($dms, $value, $errno, $errstr, 1);//@ 
  32.      $result = $fp ? '<span >开启</span>' : '<b>关闭</b>'; 
  33.      echo '<td>' . $result . '</td>'; 
  34.      echo '<td>-</td>'; 
  35.      echo '</tr>'; 
  36.      fclose(); 
  37.  ob_flush(); 
  38.  flush(); 
  39.  } 
  40.    
  41.  exit(); 
  42. } 
  43. ?> 
  44. <!DOCTYPE html> 
  45. <html> 
  46. <head> 
  47. <title><?php echo $t; ?></title> 
  48. <meta charset="UTF-8"> 
  49. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0"> 
  50. <meta name="apple-mobile-web-app-capable" content="yes" /> 
  51. <script type="text/javascript"> 
  52. console.log("问题反馈电话:","15058593138"); 
  53. console.log("问题反馈邮件:","admin@12391.net"); 
  54. function $(objId){ 
  55. return document.getElementById(objId); 
  56. } 
  57. function loadcha(xid) { 
  58. var xmlhttp; 
  59. var Stxt= "nums=aa"; 
  60. Stxt+="&prtprt="+ encodeURIComponent($("prt").value); 
  61. Stxt+="&domadoma="+ encodeURIComponent($("doma").value); 
  62. $("sb").disabled = true; 
  63. $("tipx").innerHTML = "正在加载(请等待)..."; 
  64. if (window.XMLHttpRequest) { 
  65. xmlhttp = new XMLHttpRequest(); 
  66. } else { 
  67. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
  68. } 
  69. xmlhttp.onreadystatechange = function() { 
  70. if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
  71. var btxt = xmlhttp.response; $("sb").disabled = false; 
  72. if(btxt == "err01"){ $("tipx").innerHTML = "!"; return false;} 
  73. $('tipx').innerHTML = xmlhttp.response; 
  74. } 
  75. } 
  76. xmlhttp.open("POST", "?x=cha&tt="+Math.random(), true); 
  77. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
  78. xmlhttp.send(Stxt); 
  79. }  
  80. </script> 
  81. <style> 
  82. div,#tipx{display:block;width:99.7%;border:0;margin-top:5px;} 
  83. textarea{display:block;width:99.7%;border:1px solid #ccc;height:160px;} 
  84. table{margin:20px auto;border-left:1px solid #a2c6d3;border-top:3px solid #0180CF;font-size:12px;width:99.7%;} 
  85. table td{border-right:1px solid #a2c6d3;border-bottom:1px solid #a2c6d3;padding:2px;word-wrap:break-word;word-break:break-all;} 
  86. td{min-width:30px;max-width:490px;} 
  87. .tt{background-color: #f2f2f2;} 
  88. b{color:red;} span{color:green;} 
  89. #sb{ height:35px;} 
  90. </style> 
  91. </head> 
  92. <body>  
  93. <p><?php echo $t; ?></p> 
  94. <input type="input" id="doma" value="<?php echo $ip; ?>" > 
  95. <textarea id="prt"><?php echo join(",",$prt); ?></textarea> 
  96. <input type="button" id="sb" value="提交分析" onclick="loadcha('xid')"> 
  97. <table> 
  98. <thead><?php echo "<tr class='tt'><td>".join("</td><td>",$xx)."</td></tr>"; ?></thead> 
  99. <tbody id="tipx"></tbody> 
  100. </table> 
  101. </body> 
  102. </html> 

方法补充

除了上文的方法,小编还为大家整理了其他PHP实现端口批量查询的方法,希望对大家有所帮助。

PHP单程序文件实现IP归属地批量查询

支持提取IP便于直接粘贴日志,去重防止重复查询浪费计算资源

按出现次数排序方便分析该IP行为。查询速度极快。

  1. <?php  
  2. //整理与分享:yujianyue<15058593138@qq.com> 
  3. //注意还得放纯真IP数据库 qqwry.dat(自行百度下载最新版) 和本查询代码同级  
  4. //核心查询程序 class IpLocation 来自网络 
  5. function tipx($str){ 
  6. preg_match_all('/\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/', $str, $match); 
  7. //$uni = array_unique($match[0]); //asort($uni); 
  8. $uni = array_count_values($match[0]); // 使用array_count_values函数统计数组中每个元素的数量 
  9.   arsort($uni); 
  10. return $uni;//得结果,可输出查看或调用 
  11. } 
  12.    
  13.  class IpLocation { 
  14.  //数据文件指针 
  15.  var $fp; 
  16.  var $firstip; 
  17.  var $lastip; 
  18.  var $totalip;  
  19.  function getlong() { 
  20.  //unpack从二进制字符串对数据进行解包 
  21.  //将读取的little-endian编码的4个字节转化为长整型数,fread安全读取二进制文件 
  22.  $result = unpack('Vlong', fread($this->fp, 4)); 
  23.  return $result['long']; 
  24.  } 
  25.  function getlong3() { 
  26.  //将读取的little-endian编码的3个字节转化为长整型数 
  27.  $result = unpack('Vlong', fread($this->fp, 3).chr(0)); 
  28.  return $result['long']; 
  29.  } 
  30.     
  31.  function packip($ip) { 
  32.  //pack把数据装入一个二进制字符串 
  33.  //ip2long将IP地址转成无符号的长整型,也可以用来验证IP地址 
  34.  return pack('N', intval(ip2long($ip))); 
  35.  } 
  36.    
  37.  function getstring($data = "") { 
  38.  $char = fread($this->fp, 1); 
  39.  while (ord($char) > 0) { //ord返回字符的ASCII值,字符串按照C格式保存,以\0结束 
  40.  $data .= $char; 
  41.  $char = fread($this->fp, 1); 
  42.  } 
  43.  return $data; 
  44.  } 
  45.    
  46.  function getarea() { 
  47.  $byte = fread($this->fp, 1); // 标志字节 
  48.  switch (ord($byte)) { 
  49.  case 0: // 没有区域信息 
  50.  $area = ""; 
  51.  break; 
  52.  case 1: 
  53.  case 2: // 标志字节为1或2,表示区域信息被重定向 
  54.  fseek($this->fp, $this->getlong3()); 
  55.  $area = $this->getstring(); 
  56.  break; 
  57.  default: // 否则,表示区域信息没有被重定向 
  58.  $area = $this->getstring($byte); 
  59.  break; 
  60.  } 
  61.  return $area; 
  62.  } 
  63.    
  64.  function getlocation($ip) { 
  65.  if (!$this->fp) return null; // 如果数据文件没有被正确打开,则直接返回空 
  66.  $location['ip'] = gethostbyname($ip); // 域名转化为IP地址 
  67.  $ip = $this->packip($location['ip']); // 将输入的IP地址转化为可比较的IP地址 
  68.  // 不合法的IP地址会被转化为255 
  69.  // 对分搜索 
  70.  $l = 0; // 搜索的下边界 
  71.  $u = $this->totalip; // 搜索的上边界 
  72.  $findip = $this->lastip; // 如果没有找到就返回最后一条IP记录(QQWry.Dat的版本信息) 
  73.  while ($l <= $u) { // 当上边界小于下边界时,查找失败 
  74.  $i = floor(($l + $u) / 2); // 计算近似中间记录 
  75.  fseek($this->fp, $this->firstip + $i * 7); 
  76.  $beginip = strrev(fread($this->fp, 4)); // 获取中间记录的开始IP地址,strrev反转字符串 
  77.  // strrev函数在这里的作用是将little-endian的压缩IP地址转化为big-endian的格式,便于比较 
  78.  //关于little-endian与big-endian 参考:http://baike.baidu.com/view/2368412.htm 
  79.  if ($ip < $beginip) { // 用户的IP小于中间记录的开始IP地址时 
  80.  $u = $i - 1; // 将搜索的上边界修改为中间记录减一 
  81.  } 
  82.  else { 
  83.  fseek($this->fp, $this->getlong3()); 
  84.  $endip = strrev(fread($this->fp, 4)); // 获取中间记录的结束IP地址 
  85.  if ($ip > $endip) { // 用户的IP大于中间记录的结束IP地址时 
  86.  $l = $i + 1; // 将搜索的下边界修改为中间记录加一 
  87.  } 
  88.  else { // 用户的IP在中间记录的IP范围内时 
  89.  $findip = $this->firstip + $i * 7; 
  90.  break; // 则表示找到结果,退出循环 
  91.  } 
  92.  } 
  93.  } 
  94.    
  95.  fseek($this->fp, $findip); 
  96.  $location['beginip'] = long2ip($this->getlong()); // 用户IP所在范围的开始地址 
  97.  $offset = $this->getlong3(); 
  98.  fseek($this->fp, $offset); 
  99.  $location['endip'] = long2ip($this->getlong()); // 用户IP所在范围的结束地址 
  100.  $byte = fread($this->fp, 1); // 标志字节 
  101.  switch (ord($byte)) { 
  102.  case 1: // 标志字节为1,表示国家和区域信息都被同时重定向 
  103.  $countryOffset = $this->getlong3(); // 重定向地址 
  104.  fseek($this->fp, $countryOffset); 
  105.  $byte = fread($this->fp, 1); // 标志字节 
  106.  switch (ord($byte)) { 
  107.  case 2: // 标志字节为2,表示国家信息又被重定向 
  108.  fseek($this->fp, $this->getlong3()); 
  109.  $location['country'] = $this->getstring(); 
  110.  fseek($this->fp, $countryOffset + 4); 
  111.  $location['area'] = $this->getarea(); 
  112.  break; 
  113.  default: // 否则,表示国家信息没有被重定向 
  114.  $location['country'] = $this->getstring($byte); 
  115.  $location['area'] = $this->getarea(); 
  116.  break; 
  117.  } 
  118.  break; 
  119.  case 2: // 标志字节为2,表示国家信息被重定向 
  120.  fseek($this->fp, $this->getlong3()); 
  121.  $location['country'] = $this->getstring(); 
  122.  fseek($this->fp, $offset + 8); 
  123.  $location['area'] = $this->getarea(); 
  124.  break; 
  125.  default: // 否则,表示国家信息没有被重定向 
  126.  $location['country'] = $this->getstring($byte); 
  127.  $location['area'] = $this->getarea(); 
  128.  break; 
  129.  } 
  130.  if ($location['country'] == " CZNET") { // CZNET表示没有有效信息 
  131.  $location['country'] = "未知"; 
  132.  } 
  133.  if ($location['area'] == " CZNET") { 
  134.  $location['area'] = ""; 
  135.  } 
  136.  return $location; 
  137.  } 
  138.  /** 
  139.  * 构造函数,打开 QQWry.Dat 文件并初始化类中的信息 
  140.  */ 
  141.  function __construct($filename = "./qqwry.dat") { 
  142.  $this->fp = 0; 
  143.  if (($this->fp = @fopen($filename, 'rb')) !== false) { 
  144.  $this->firstip = $this->getlong(); 
  145.  $this->lastip = $this->getlong(); 
  146.  $this->totalip = ($this->lastip - $this->firstip) / 7; 
  147.  //注册析构函数,使其在程序执行结束时执行 
  148.  register_shutdown_function(array(&$this, '_IpLocation')); 
  149.  } 
  150.  } 
  151.  /** 
  152.  * 析构函数,用于在页面执行结束后自动关闭打开的文件 
  153.  */ 
  154.  function _IpLocation() { 
  155.  if ($this->fp) { 
  156.  fclose($this->fp); 
  157.  } 
  158.  $this->fp = 0; 
  159.  } 
  160.  }  
  161. if($_GET["x"] == "cha"){ 
  162. $tips = isset($_POST['tips']) ? $_POST['tips'] : ''; 
  163. //if(!filter_var($user, FILTER_VALIDATE_EMAIL)){ exit("电子邮件格式错误");} 
  164. $txts = str_replace(array("\r\n","\r","\n","\t",","," "),"|",$tips); 
  165. $lisa = tipx($txts); //explode("|",$txts); 
  166. echo "<h2><strong>以下信息仅供参考</strong>(提取IP/已去重/按出现次数降序排序):</h2>\r\n"; 
  167. echo "<table cellspacing=\"0\" class=\"table\" cellpadding=\"0\">\r\n"; 
  168. echo "<tr class='tt'><td width='99'>IP</td><td width='60'>出现次数</td><td width='99'>IP/24</td><td>归属地</td></tr>\r\n"; 
  169. $iplocation = new IpLocation(); $ipx= array(); 
  170. foreach($lisa as $zz => $ges){ 
  171.  $location = $iplocation->getlocation($zz); 
  172.  $zi = explode(".",$zz); $zip = $zi[0].".".$zi[1].".".$zi[2].".0/24"; 
  173.  $diqu = mb_convert_encoding($location["country"]."_".$location["area"], "utf-8", "gbk"); 
  174.  echo "<tr><td>$zz</td><td>$ges</td><td>$zip</td><td>$diqu</td></tr>\r\n"; 
  175. } 
  176. echo "</table>\r\n"; 
  177. exit(); 
  178. } 
  179. ?> 
  180. <!DOCTYPE html> 
  181. <html> 
  182. <head> 
  183. <title>批量查IP归属地</title> 
  184. <meta charset="UTF-8"> 
  185. <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0"> 
  186. <meta name="apple-mobile-web-app-capable" content="yes" /> 
  187. <script type="text/javascript"> 
  188. console.log("问题反馈电话:","15058593138"); 
  189. console.log("问题反馈邮件:","admin@12391.net"); 
  190. function $(objId){ 
  191. return document.getElementById(objId); 
  192. } 
  193. function loadcha(xid) { 
  194. var xmlhttp; 
  195. var Stxt= "nums=aa"; 
  196. Stxt+="&tipstips="+ encodeURIComponent($("tips").value); 
  197. //$("tips").innerHTML = "正在加载..."; 
  198. if (window.XMLHttpRequest) { 
  199. xmlhttp = new XMLHttpRequest(); 
  200. } else { 
  201. xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
  202. } 
  203. xmlhttp.onreadystatechange = function() { 
  204. if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
  205. var btxt = xmlhttp.response; 
  206. if(btxt == "err01"){ $("tipx").innerHTML = "!"; return false;} 
  207. $('tipx').innerHTML = xmlhttp.response; 
  208. } 
  209. } 
  210. xmlhttp.open("POST", "?x=cha&tt="+Math.random(), true); 
  211. xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
  212. xmlhttp.send(Stxt); 
  213. } 
  214.    
  215. </script> 
  216. <style> 
  217. div,#tipx{display:block;width:99.7%;border:0;margin-top:5px;} 
  218. textarea{display:block;width:99.7%;border:1px solid #ccc;height:160px;} 
  219. table{margin:20px auto;border-left:1px solid #a2c6d3;border-top:3px solid #0180CF;font-size:12px;width:99.7%;} 
  220. table td{border-right:1px solid #a2c6d3;border-bottom:1px solid #a2c6d3;padding:2px;word-wrap:break-word;word-break:break-all;} 
  221. td{min-width:30px;max-width:490px;} 
  222. .tt{background-color: #f2f2f2;} 
  223. #submit{ height:35px;} 
  224. </style> 
  225. </head> 
  226. <body> 
  227.    
  228. <form class="form" id="form" method="POST" act="?act=cha" > 
  229.  <h3>批量查询IP归属地</h3> 
  230. <p>自动提文本中IP地址去重和出现次数降序并查询归属地!</p> 
  231. <textarea id="tips"> 
  232. 3.224.220.24 
  233. 52.70.240.24 
  234. 202.63.172.24 
  235. </textarea> 
  236.  <input type="button" id="submit" value="提交查询" onclick="loadcha('xid')"> 
  237. <div id="tipx"></div> 
  238.  </form> 
  239. </body> 
  240. </html>

Tags: PHP端口批量查询工具

分享到: