Java 语言实现 IPv6 是否ping通

Posted 龙凌云端

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java 语言实现 IPv6 是否ping通相关的知识,希望对你有一定的参考价值。

Java 语言实现 IPv6 是否ping通


 

1、导入相关的 Java 网络库

import java.net.InetAddress;
import java.net.UnknownHostException;

 

2、定义 ping 方法,传入目标 IPv6 地址

public static boolean ping(String ipv6Address) 
   boolean isPingSuccessful = false;
   try 
      InetAddress inet = InetAddress.getByName(ipv6Address);
      isPingSuccessful = inet.isReachable(5000);
    catch (UnknownHostException e) 
      e.printStackTrace();
    catch (IOException e) 
      e.printStackTrace();
   
   return isPingSuccessful;

 

3、在方法中,使用 InetAddress 类中的 getByName 方法获取目标 IPv6 地址的 InetAddress 实例。

4、使用 InetAddress 实例的 isReachable 方法来判断是否能够连接到目标 IPv6 地址。如果能够连接,则 isReachable 方法返回 true,否则返回 false。

5、使用 try-catch 语句来捕捉 UnknownHostException 和 IOException 异常。

6、最后,返回 ping 是否成功的 boolean 值。

 

示例代码如下:

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.io.IOException;

public class IPv6Ping 
   public static boolean ping(String ipv6Address) 
      boolean isPingSuccessful = false;
      try 
         InetAddress inet = InetAddress.getByName(ipv6Address);
         isPingSuccessful = inet.isReachable(5000);
       catch (UnknownHostException e) 
         e.printStackTrace();
       catch (IOException e) 
         e.printStackTrace();
      
      return isPingSuccessful;
   
   
   public static void main(String[] args) 
      String ipv6Address = "2001:4860:4860::8888";
      boolean isPingSuccessful = ping(ipv6Address);
      if(isPingSuccessful) 
         System.out.println("Ping " + ipv6Address + " successful!");
       else 
         System.out.println("Ping " + ipv6Address + " failed!");
      
   

 

运行结果如下:

Ping 2001:4860:4860::8888 successful!

 

批量ping IP地址命令测试是否通

 

批量ping IP地址命令测试是否通

 

#!/bin/bash
#--------------------------------------------------------------------------------------------------
echo -e " "
echo -e " ==============================================="
echo -e "| Thanks for using this script! |"
echo -e " ==============================================="
echo -e "#***********************************************#"
echo -e "# ___ ___ _ __ ___ _ __ _ _ #"
echo -e "# / __/ _ \| ‘_| | \| |_ \| | | | #"
echo -e "# | (_| (_) | | | | | | |_) | |_| | #"
echo -e "# \___\___/|_| |_| |_| .__/ \__, | #"
echo -e "# |_| |___/ #"
echo -e "#-----------------------------------------------#"
echo -e "# #"
echo -e "# Author: compy QQ: 2071959367 #"
echo -e "# #"
echo -e "#***********************************************#"
echo -e " "

#--------------------------------------------------------------------------------------------------
#System environment variables.
export PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

#--------------------------------------------------------------------------------------------------
echo "=================================英文注释========================================="
echo -e " This script determines whether the IP address is Ping,\n and the IP of Ping is stored in the ping.tong file in the current directory.\n The Ping IP is stored in the ping.notong file under the current directory."
echo "================================================================================"
echo "=================================中文注释========================================="
echo -e "此脚本为判断IP地址是否ping通,\nping通的IP存放在当前目录下的ping.tong文件中,\nping不通的IP存放在当前目录下的ping.notong文件中."
echo "================================================================================"
echo " "
if [ -z $1 ]
then
echo "Please enter the file name(eg:/home/eg.txt):"
read lu
else
lu=$1
fi

rq=`date +%G%m%d%k%m%S`
tar -zcf ip_ping_${rq}.tar.gz ip_ping
rm -rf ip_ping/
mkdir -p ip_ping
ULIST=$(cat $lu)
for UNAME in $ULIST
do
echo $UNAME >> a.txtlog
ping $UNAME -c 3 -i 0.01 >> ip_ping/$UNAME
#ping $UNAME -c 10 -i 0.01
number=`cat ip_ping/$UNAME | grep transmitted | awk -F "," ‘{print $3}‘ | awk -F " " ‘{print $1}‘ | awk -F "%" ‘{print $1}‘`
echo "$UNAME $number" >> ./ip_txt.txt
if [ $number = 100 ]
then
echo "$UNAME ping不通" >>./ping.tong
echo "$UNAME ping不通"
else
echo "$UNAME ping通" >>./ping.notong
echo "$UNAME ping通"
fi
rm -rf ip_ping/$UNAME
done

 

源码文件链接:http://pan.baidu.com/s/1c20B8XE

 

作者:compy    更新日期:2017-07-30 23:07    QQ:2071959367

 

以上是关于Java 语言实现 IPv6 是否ping通的主要内容,如果未能解决你的问题,请参考以下文章

为啥我可以ping通别人,别人ping不通我?

电信ipv6,公网为啥ping不通主机,但主机可以访问公网

为啥获取了ipv6地址却无法访问纯ipv6网站

两台电脑之间怎么互相ping

使用for和while编写校本ping通网段

62-高级路由:IPv6 to 4自动隧道