1

目前我在他们的 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);
}
4

2 回答 2

0

您可以使用 awk 处理host命令的输出,并在 PHP 中使用exec(). 结果将是一个 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
)
于 2012-11-03T23:44:56.790 回答
0

好的,所以诀窍在于用 URL 中的 IP 替换主机名,同时添加带有主机名的标头

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

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;
}
于 2012-11-04T14:47:26.327 回答