最近要获取一批域名的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记录,并且返回了

完整的参考文件:

<!doctype html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PING测试</title>
</head>
<body>
<?
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 "<br>";
   }
 }
 echo "IP个数:".count($ips);
}

$my=$default;
?>
<hr>
<form method=post action="">
域名列表:<br />
<textarea name="my" rows="10" cols="70"><?echo implode("\n",$my);?></textarea>
<br /><input type="submit">
</form>
</body>
</html>


提交得到的结果:

引用
www.aslibra.com : 220.162.244.47
aslibra.com : 218.85.132.229
online.aslibra.com : 60.28.197.17 [cname online.zcom.com]
IP个数:3



原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
Tags:
djks Email
2010/07/23 12:31
我用gethostbynama很方便,不过就是有点慢
djks Email
2010/07/22 16:23
我测试这个代码不能获取IP呢

如果用gethostbyname处理会不会方便些?
hqlulu 回复于 2010/07/22 18:51
这个你看看domain2ip函数里,字符匹配可能在linux下和win下是不一样的返回
这个主要是想知道是否有cname的设置,gethostbyname的确是方便的做法
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐