一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

PHP处理Ping命令获取批量域名的IP

时间:2010-01-13 编辑:简简单单 来源:一聚教程网

PHP处理Ping命令获取批量域名的IP


PHP来处理也OK,其它我不会,那就用它了,调用系统命令是使用 exec ,先看看介绍:


引用
exec -- Execute an external program
说明
string exec ( string command [, array &output [, int &return_var]] )

exec() executes the given command.

参数

command
The command that will be executed.

output
If the output argument is present, then the specified array will be filled with every line of output from the command. Trailing whitespace, such as n, is not included in this array. Note that if the array already contains some elements, exec() will append to the end of the array. If you do not want the function to append elements, call unset() on the array before passing it to exec().

return_var
If the return_var argument is present along with the output argument, then the return status of the executed command will be written to this variable.

返回值
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.

To get the output of the executed command, be sure to set and use the output parameter.


那我们执行系统命令ping就可以,返回结果回来处理就好

看看执行效果:


引用

Pinging www.aslibra.com [220.162.244.47] with 32 bytes of data:

Reply from 220.162.244.47: bytes=32 time=42ms TTL=119
Reply from 220.162.244.47: bytes=32 time=40ms TTL=119
Reply from 220.162.244.47: bytes=32 time=40ms TTL=119
Reply from 220.162.244.47: bytes=32 time=40ms TTL=119

...


那我们取出第二行就可以得到ip了,但也有cname的域名,比如
ping online.aslibra.com


引用

Pinging online.zcom.com [60.28.197.17] with 32 bytes of data:

Reply from 60.28.197.17: bytes=32 time=5ms TTL=57
Reply from 60.28.197.17: bytes=32 time=4ms TTL=57
Reply from 60.28.197.17: bytes=32 time=4ms TTL=57
Reply from 60.28.197.17: bytes=32 time=4ms TTL=57


这种情况是域名不一样的,所以,可以区分开,那执行一次ping就可以了,命令是 ping domain -n 1

所以就可以把以上事情写成功能函数啦:


function domain2ip($domain){
 exec("ping $domain -n 1",$a);
 if (ereg ("Pinging (.*) [(.*)]", $a[1], $regs)) {
   if($regs[1]!=$domain){
     return Array("domain"=>$regs[1],"ip"=>$regs[2]);
   }else{
     return Array("domain"=>"","ip"=>$regs[2]);
   }
 }
 return Array();
}


返回的是数组,如果是空数组则域名无效,如果是domain没有指定,那就是A记录,否则是cname记录,并且返回了

完整的参考文件:


http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">


PING测试


function domain2ip($domain){
 exec("ping $domain -n 1",$a);
 if (ereg ("Pinging (.*) [(.*)]", $a[1], $regs)) {
   if($regs[1]!=$domain){
     return Array("domain"=>$regs[1],"ip"=>$regs[2]);
   }else{
     return Array("domain"=>"","ip"=>$regs[2]);
   }
 }
 return Array();
}

$my=trim($_POST["my"]);
$my=explode("n",$my);

$default=array();
$default[]="www.aslibra.com";
$default[]="aslibra.com";
$default[]="online.aslibra.com";
$default[]="www.nodomaintest.com";

//ping
if(count($my)){
 $ips=array();
 foreach($my as $v){
   $v=trim($v);
   $result=domain2ip($v);
   if($v){
     $ips[$result["ip"]]++;
     echo $v." : ".$result["ip"];
     if($result["domain"]) echo " [cname ".$result["domain"]."] ";
     echo "
";
   }
 }
 echo "IP个数:".count($ips);
}

$my=$default;
?>




域名列表:






实例二
php  
   
      $ip   =   "www.111com.net";  
      exec("ping   $ip",   $arr);  
   
      print("

");&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print_r($arr);&nbsp;&nbsp; <br /> &nbsp;&nbsp;&nbsp; die();<br /> &nbsp; ?><br /> &nbsp; 输入结果<br /> &nbsp; Array<br /> (<br /> &nbsp;&nbsp;&nbsp; [0] => <br /> &nbsp;&nbsp;&nbsp; [1] => Pinging <a href="http://www.111com.net">www.111com.net</a> [61.152.144.58] with 32 bytes of data:<br /> &nbsp;&nbsp;&nbsp; [2] => <br /> &nbsp;&nbsp;&nbsp; [3] => Reply from 61.152.144.58: bytes=32 time=36ms TTL=118<br /> &nbsp;&nbsp;&nbsp; [4] => Reply from 61.152.144.58: bytes=32 time=37ms TTL=118<br /> &nbsp;&nbsp;&nbsp; [5] => Reply from 61.152.144.58: bytes=32 time=39ms TTL=118<br /> &nbsp;&nbsp;&nbsp; [6] => Reply from 61.152.144.58: bytes=32 time=38ms TTL=118<br /> &nbsp;&nbsp;&nbsp; [7] => <br /> &nbsp;&nbsp;&nbsp; [8] => Ping statistics for 61.152.144.58:<br /> &nbsp;&nbsp;&nbsp; [9] =>&nbsp;&nbsp;&nbsp;&nbsp; Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),<br /> &nbsp;&nbsp;&nbsp; [10] => Approximate round trip times in milli-seconds:<br /> &nbsp;&nbsp;&nbsp; [11] =>&nbsp;&nbsp;&nbsp;&nbsp; Minimum = 36ms, Maximum = 39ms, Average = 37ms<br /> )</p> </div> <ul class="TurnPage"> <li class="TurnPage-left"> <p> <span>上一个:</span> <a href="https://www.111com.net/phper/19/54ffb1ac8a60efa56dbb455a72a8a83b.htm" class="maxWidth">mysql sql mode </a> </p> </li> <li class="TurnPage-right"> <p> <span>下一个:</span> <a href="https://www.111com.net/phper/30/65b0bb54deac74cbb2eedf1798118c56.htm" class="maxWidth">php 汉字转换拼音程序</a> </p> </li> </ul> <div class="articles"> <div class="tit02"> <h4>相关文章</h4> </div> <ul> <li> <a target="_blank" href="https://www.111com.net/phper/228481.htm">PHP导出数据超时的优化建议解读</a> <span>10-31</span> </li> <li> <a target="_blank" href="https://www.111com.net/phper/228478.htm">PHP之mysql位运算解析</a> <span>10-31</span> </li> <li> <a target="_blank" href="https://www.111com.net/phper/228475.htm">Laravel实现登录跳转功能解析</a> <span>10-31</span> </li> <li> <a target="_blank" href="https://www.111com.net/phper/228473.htm">php双向队列解读</a> <span>10-31</span> </li> <li> <a target="_blank" href="https://www.111com.net/phper/226305.htm">Laravel异常上下文解决教程</a> <span>10-24</span> </li> <li> <a target="_blank" href="https://www.111com.net/phper/226295.htm">php数组查询元素位置方法介绍</a> <span>10-24</span> </li> </ul> </div> </div> </div> </div> </div> <div class="hot-column"> <div class="cont"> <div class="tit"> <h4>热门栏目</h4> </div> <ul class="clearfix"> <li> <h6><a href="https://www.111com.net/phper/php.html" target="_blank">php教程</a></h6> <a href="https://www.111com.net/list-45/" target="_blank">php入门</a> <a href="https://www.111com.net/list-46/" target="_blank">php安全</a> <a href="https://www.111com.net/list-47/" target="_blank">php安装</a> <a href="https://www.111com.net/list-48/" target="_blank">php常用代码</a> <a href="https://www.111com.net/list-49/" target="_blank">php高级应用</a> </li> <li> <h6><a href="https://www.111com.net/net/net.html" target="_blank">asp.net教程</a></h6> <a href="https://www.111com.net/list-78/" target="_blank">基础入门</a> <a href="https://www.111com.net/list-79/" target="_blank">.Net开发</a> <a href="https://www.111com.net/list-80/" target="_blank">C语言</a> <a href="https://www.111com.net/list-81/" target="_blank">VB.Net语言</a> <a href="https://www.111com.net/list-82/" target="_blank">WebService</a> </li> <li> <h6><a href="https://www.111com.net/sj/index.html" target="_blank">手机开发</a></h6> <a href="https://www.111com.net/list-208/" target="_blank">安卓教程</a> <a href="https://www.111com.net/list-209/" target="_blank">ios7教程</a> <a href="https://www.111com.net/list-210/" target="_blank">Windows Phone</a> <a href="https://www.111com.net/list-211/" target="_blank">Windows Mobile</a> <a href="https://www.111com.net/list-212/" target="_blank">手机常见问题</a> </li> <li> <h6><a href="https://www.111com.net/cssdiv/css.html" target="_blank">css教程</a></h6> <a href="https://www.111com.net/list-99/" target="_blank">CSS入门</a> <a href="https://www.111com.net/list-100/" target="_blank">常用代码</a> <a href="https://www.111com.net/list-101/" target="_blank">经典案例</a> <a href="https://www.111com.net/list-102/" target="_blank">样式布局</a> <a href="https://www.111com.net/list-103/" target="_blank">高级应用</a> </li> <li> <h6><a href="https://www.111com.net/wy/yw.html" target="_blank">网页制作</a></h6> <a href="https://www.111com.net/list-136/" target="_blank">设计基础</a> <a href="https://www.111com.net/list-137/" target="_blank">Dreamweaver</a> <a href="https://www.111com.net/list-138/" target="_blank">Frontpage</a> <a href="https://www.111com.net/list-139/" target="_blank">js教程</a> <a href="https://www.111com.net/list-140/" target="_blank">XNL/XSLT</a> </li> <li> <h6><a href="https://www.111com.net/office/index.html" target="_blank">办公数码</a></h6> <a href="https://www.111com.net/list-236/" target="_blank">word</a> <a href="https://www.111com.net/list-237/" target="_blank">excel</a> <a href="https://www.111com.net/list-238/" target="_blank">powerpoint</a> <a href="https://www.111com.net/list-239/" target="_blank">金山WPS</a> <a href="https://www.111com.net/list-240/" target="_blank">电脑新手</a> </li> <li> <h6><a href="https://www.111com.net/jsp/jsp.html" target="_blank">jsp教程</a></h6> <a href="https://www.111com.net/list-68/" target="_blank">Application与Applet</a> <a href="https://www.111com.net/list-69/" target="_blank">J2EE/EJB/服务器</a> <a href="https://www.111com.net/list-70/" target="_blank">J2ME开发</a> <a href="https://www.111com.net/list-71/" target="_blank">Java基础</a> <a href="https://www.111com.net/list-72/" target="_blank">Java技巧及代码</a> </li> </ul> </div> </div> <div class="footer"> <div class="cont"> <p> <a href="https://www.111com.net/" target="_self">一聚教程网</a>| <a href="https://www.111com.net/us/us.html" class="about" target="_self">关于我们</a>| <a href="https://www.111com.net/us/me.html" class="contact" target="_self">联系我们</a>| <a href="https://www.111com.net/us/ads.html" class="gg_contact" target="_self">广告合作</a>| <a href="https://www.111com.net/us/link.html" class="friend_link" target="_self">友情链接</a>| <a href="https://www.111com.net/us/bcinfo.html" class="copyright_notice" target="_self">版权声明</a> </p> <p> <span>copyRight@2007-2024 www.111COM.NET AII Right Reserved <a href="https://beian.miit.gov.cn/" target="_blank" class="beian">苏ICP备17065847号-2</a> </span> </p> <p> <span> 网站内容来自网络整理或网友投稿如有侵权行为请邮件:yijucomnet@163.com 我们24小时内处理 </span> </p> </div> </div> <script src="https://assets.111com.net/js/stat.js?v=2024022101"></script> <script src="https://api.111com.net/api/stat/hits?type=article&id=31197"></script> </body> </html>