目前,我使用其IPv4和IPv6地址监控我的网站
但我的下一个网站将托管多个IP地址。我想 监控所有这些 。
这是我目前的双栈监控代码(仅监控一个IP地址)
<?php
$resultat4 = file_get_contents_curl( 'http://www.mydomain.com/?cjg_monitoring=1', 4);
$resultat6 = file_get_contents_curl( 'http://www.mydomain.com/?cjg_monitoring=1', 6);
if (substr($resultat4,0,2) == "OK"){ echo 'IPv4 stack OK' }
if (substr($resultat6,0,2) == "OK"){ echo 'IPv6 stack OK' }
function file_get_contents_curl($url,$ip_version=0) {
$ch = curl_init();
if($ip_version == 4)
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
elseif($ip_version == 6)
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6 );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
?>
如何使用Curl PHP测试所有测试的IP地址?
我想做的事情与这个更简单的版本类似:
<?php
$ip_adds = get_all_ip_addresses( $url );
$problem_encountered = false;
foreach( $ip_adds as $ip_add ){
$content = get_content_url_on_ip( 'http://www.mydomain.com/?cjg_monitoring=1', $ip_add );
if ( $content != "OK") $problem_encountered = true;
}
if ( !$problem_encountered ) echo "All IPv4 and IPv6 Addresses OK";
?>
在我的情况下www.mydomain.com有许多每个版本的IP地址:
$host www.mydomain.com
www.mydomain.com has address 173.194.35.180
www.mydomain.com has address 173.194.35.176
www.mydomain.com has address 173.194.35.177
www.mydomain.com has IPv6 address 2001:db8:4016:801::1008
www.mydomain.com has IPv6 address 2001:db8:4016:801::1009
www.mydomain.com has IPv6 address 2001:db8:4016:801::1010
这就是我使用它的原因:
//return array[0][ip] or array[0][ipv6]
function get_all_ip_addresses( $url ){
$url_tab = parse_url($url);
$host = $url_tab['host']
$ip4s = dns_get_record($host, DNS_A);
$ip6s = dns_get_record($host, DNS_AAAA);
return array_merge($ip4s, $ip6s);
}
答案 0 :(得分:0)
您可以使用awk处理host
命令的输出,并使用exec()
在PHP中调用它。结果将是一组IP地址,包括IPv4和IPv6 - 如果需要,您可以使用正则表达式来确定哪个是。
function get_all_ip_addresses($host){
exec('host ' . $host . ' | awk \'/has (IPv6 )?address/ { if ($3 == "IPv6") print $5; else print $4; }\'', $ips);
return $ips;
}
阵列转储:
Array
(
[0] => 173.194.35.180
[1] => 173.194.35.176
[2] => 173.194.35.177
[3] => 2001:db8:4016:801::1008
[4] => 2001:db8:4016:801::1009
[5] => 2001:db8:4016:801::1010
)
答案 1 :(得分:0)
好的,所以诀窍在于用URL中的IP替换主机名,同时添加带主机名的标题
注意:http://192.168.0.1/?var=1
使用ipv6
http://[2001:db8::1]/?var=1
function file_get_contents_url_ip($url,$host_ip=null,$ip_version=0,$ignore_certificate=false) {
$ch = curl_init();
if (!is_null($host_ip)){
$urldata = parse_url($url);
// Ensure we have the query too, if there is any...
if (!empty($urldata['query']))
$urldata['path'] .= "?".$urldata['query'];
// Specify the host (name) we want to fetch...
$headers = array("Host: ".$urldata['host']);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// create the connecting url (with the hostname replaced by IP)
if( $ip_version == 6 )
$url = $urldata['scheme']."://[".$host_ip.']'.$urldata['path'];
else
$url = $urldata['scheme']."://".$host_ip.$urldata['path'];
//ssl certificate would fail because https://192.168.0.1 is used instead of hostname
$ignore_certificate = 1;
}
if( $ip_version == 4 )
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );
elseif( $ip_version == 6 )
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V6 );
if( $ignore_certificate )
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false );
else
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true );
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
curl_setopt($ch, CURLOPT_URL, $url);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}