最近要获取一批域名的IP列表,所以阿权想着要写些代码来做,懒得我一个一个处理了。
PHP来处理也OK,其它我不会,那就用它了,调用系统命令是使用 exec ,先看看介绍:
那我们执行系统命令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
所以就可以把以上事情写成功能函数啦:
返回的是数组,如果是空数组则域名无效,如果是domain没有指定,那就是A记录,否则是cname记录,并且返回了
完整的参考文件:
提交得到的结果:
原创内容如转载请注明:来自 阿权的书房
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.
说明
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();
}
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>
<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
aslibra.com : 218.85.132.229
online.aslibra.com : 60.28.197.17 [cname online.zcom.com]
IP个数:3
原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
十二星座学生作弊方法
php日历的高效写法

如果用gethostbyname处理会不会方便些?
这个主要是想知道是否有cname的设置,gethostbyname的确是方便的做法