PHP处理Ping命令获取批量域名的IP
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记录,并且返回了
完整的参考文件:
<!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>
实例二
<?php
$ip = "www.226511.com";
exec("ping $ip", $arr);
print("<xmp>");
print_r($arr);
die();
?>
输入结果
Array
(
[0] =>
[1] => Pinging www.226511.com [61.152.144.58] with 32 bytes of data:
[2] =>
[3] => Reply from 61.152.144.58: bytes=32 time=36ms TTL=118
[4] => Reply from 61.152.144.58: bytes=32 time=37ms TTL=118
[5] => Reply from 61.152.144.58: bytes=32 time=39ms TTL=118
[6] => Reply from 61.152.144.58: bytes=32 time=38ms TTL=118
[7] =>
[8] => Ping statistics for 61.152.144.58:
[9] => Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
[10] => Approximate round trip times in milli-seconds:
[11] => Minimum = 36ms, Maximum = 39ms, Average = 37ms
)
补充:Php教程,Php入门