user3818576
user3818576

Reputation: 3391

check if ip6 is down

I'm trying to check if ip6 is not connected in my code using php. My idea now is to calculate the loading if it is going to connect. I try to use this code.

$file = file_get_contents('http://ip6.me/');

// Trim IP based on HTML formatting
$pos = strpos( $file, '+3' ) + 3;
$ip = substr( $file, $pos, strlen( $file ) );

// Trim IP based on HTML formatting
$pos = strpos( $ip, '</' );
$ip = substr( $ip, 0, $pos );
$ipaddress = $ip;
$ip_get = explode('.',$ipaddress);

this work fine with me if will get ip address. But there is a time that ip6 down and it load to much. What is the best way to check if ip6 is down? I try to calculate the load of browser, but I have no idea how to do it.

Upvotes: 1

Views: 57

Answers (2)

v7d8dpo4
v7d8dpo4

Reputation: 1399

I would ping a server over IPv6. On linux, you can use the following code.

exec('ping -6 -c 1 2001:4860:4860::8888', $lines, $exit); // ping will return 0 if ping succeeds
if($exit == 0) {
    // up
    echo "up\n";
} else {
    //down
    echo "down\n";
}

EDIT:

Samuel's answer tests whether a TCP port is reachable over IPv6. In some cases, because of firewall configuration, only one of the methods may work.

Upvotes: 2

Samuel
Samuel

Reputation: 3801

You can use native PHP method socket_connect

bool socket_connect ( resource $socket , string $address [, int $port = 0 ] )

Initiate a connection to address using the socket resource socket, which must be a valid socket resource created with socket_create().

   // Creating new socket
   $socket = socket_create(AF_INET6, SOCK_STREAM, SOL_TCP)
      or die("Unable to create socket\n");

    // Trying to connect, will return True on success     
    $res = socket_connect($socket, $host, $port))

Upvotes: 1

Related Questions