萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> php編程 >> PHP處理Ping命令獲取批量域名的IP

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.111cn.net";  
      exec("ping   $ip",   $arr);  
   
      print("<xmp>");  
      print_r($arr);  
    die();
  ?>
  輸入結果
  Array
(
    [0] =>
    [1] => Pinging www.111cn.net [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
)

copyright © 萬盛學電腦網 all rights reserved