2012-11-03 78 views
2

目前我监视我的网站的IPv4和IPv6地址以编程方式监视每个IP的一个URL

但我的下一个网站将托管多个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; 
} 
?> 

我怎么能拿的所有IP地址,卷曲PHP测试?

我想怎么办就与此类似简单的版本:

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

回答

1

确定这样的特技在于在该URL通过其IP替换的主机名,并在同一时间将与所述主机名的标题反正

NB:http://192.168.0.1/?var=1变成http://[2001:db8::1]/?var=1与ipv6

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; 
} 
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 
) 
+0

谢谢,但它对我们更好e dns_get_record($ host,DNS_AAAA);我更新了我的问题,并改为标题 – chriscatfr

+0

对不起,不知道我是否真的明白你的问题... – doublesharp

+0

这只是我真的不喜欢使用exec()。我甚至禁用了我的Web服务器上的所有类似功能 – chriscatfr

相关问题