PHP handles ping commands to get the IP of the batch domain name

Source: Internet
Author: User
Tags ereg ip number trim

PHP handles ping commands to get the IP of the batch domain name


PHP to handle also OK, other I will not, then use it, call the system command is to use exec, first look at the introduction:


Reference
EXEC--Execute an external program
Description
String exec (String command [, array &output [, int &return_var]])

EXEC () executes the given command.

Parameters

Command
The command that would be executed.

Output
If The output argument is present, then the specified array would be filled with every line of output from the command. Trailing whitespace, such as N, isn't included in this array. Note this if the array already contains some elements, exec () would append to the "end of" the array. If you don't 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 would Be written to this variable.

return value
The last line from the the command. If you need to execute a command and have all of the data from the command passed directly back without any interference, use The PassThru () function.

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


Then we can execute the system command Ping, return the result and deal with it.

Look at the execution effect:


Reference

Pinging www.aslibra.com [220.162.244.47] with 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

...


Then we can get the second line of IP, but there are CNAME domain names, such as
Ping online.aslibra.com


Reference

Pinging online.zcom.com [60.28.197.17] with 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


This situation is the domain name is not the same, so, can be separated, that the implementation of a ping can be, the command is ping domain-n 1

So you can write these things into functional functions:


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 ();
}


Returns an array, if it is an empty array, the domain name is invalid, if domain is not specified, that is a record, otherwise it is CNAME records, and returns the

Complete Reference file:


<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "http://www.w3.org/TR/xhtml1/DTD/ Xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>ping Test </title>
<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 number:". Count ($ips);
}

$my = $default;
?>
<form method=post action= "" >
Domain Name list: <br/>
<textarea name= "My" rows= "ten" cols= "a" ><?echo implode ("n", $my);? ></textarea>
<BR/><input type= "Submit" >
</form>
</body>

Example Two
<?php

$ip = "Www.111cn.net";
EXEC ("Ping $ip", $arr);

Print ("<xmp>");
Print_r ($arr);
Die ();
?>
Input results
Array
(
[0] =>
[1] => Pinging www.111cn.net [61.152.144.58] with 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),
[Ten] => approximate round trips times in Milli-seconds:
[one] => Minimum = 36ms, Maximum = 39ms, Average = 37ms
)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.