java判断是否ping通(win、linux)

java实现ping

1、网上可见的两种方案

1、调用java.net.InetAddress的api

InetAddress.getByName(ipAddress).isReachable(timeout)

2、系统执行ping命令

public static boolean ping(String ipAddress, int pingTimes, int timeOut) {  
        BufferedReader in = null;  
        Runtime r = Runtime.getRuntime();  // 将要执行的ping命令,此命令是windows格式的命令  
        String pingCommand = "ping " + ipAddress + " -n " + pingTimes    + " -w " + timeOut;  
        try {   // 执行命令并获取输出  
            System.out.println(pingCommand);   
            Process p = r.exec(pingCommand);   
            if (p == null) {    
                return false;   
            }
            in = new BufferedReader(new InputStreamReader(p.getInputStream()));   // 逐行检查输出,计算类似出现=23ms TTL=62字样的次数  
            int connectedCount = 0;   
            String line = null;   
            while ((line = in.readLine()) != null) {    
                connectedCount += getCheckResult(line);   
            }   // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真  
            return connectedCount == pingTimes;  
        } catch (Exception ex) {   
            ex.printStackTrace();   // 出现异常则返回假  
            return false;  
        } finally {   
            try {    
                in.close();   
            } catch (IOException e) {    
                e.printStackTrace();   
            }  
        }
    }
    //若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
    private static int getCheckResult(String line) {  // System.out.println("控制台输出的结果为:"+line);  
        Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)",    Pattern.CASE_INSENSITIVE);  
        Matcher matcher = pattern.matcher(line);  
        while (matcher.find()) {
            return 1;
        }
        return 0; 
    }

2、常见方案的补充、优化

1、java.net.InetAddress的api,会存在数据不准的场景
例子:

PING xxx.xx.xx.xxx (xxx.xx.xx.xxx) 56(84) bytes of data.
From xxx.xx.xx.xxx icmp_seq=1 Destination Host Unreachable
From xxx.xx.xx.xxx icmp_seq=2 Destination Host Unreachable

这种结果应该是属于ping不通的,但是此api会返回成功


2、区分服务器
windows服务器下,ping的命令

ping ip -n 次数 -w 超时时间

返回示例:

正在 Ping xxx.xx.xx.xxx 具有 32 字节的数据:
来自 xxx.xx.xx.xxx 的回复: 字节=32 时间=59ms TTL=62
来自 xxx.xx.xx.xxx 的回复: 字节=32 时间=47ms TTL=62
来自 xxx.xx.xx.xxx 的回复: 字节=32 时间=56ms TTL=62

xxx.xx.xx.xxx 的 Ping 统计信息:
    数据包: 已发送 = 3,已接收 = 3,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
    最短 = 47ms,最长 = 59ms,平均 = 54ms

Linux服务器下,ping的命令

ping ip -c 次数 -W 超时时间
  • 1

注意这里的超时时间设置,是用 -W ,而非 -w !!!

返回示例:

PING xxx.xx.xx.xxx (xxx.xx.xx.xxx) 56(84) bytes of data.
64 bytes from xxx.xx.xx.xxx: icmp_seq=1 ttl=57 time=6.77 ms
64 bytes from xxx.xx.xx.xxx: icmp_seq=2 ttl=57 time=6.82 ms
64 bytes from xxx.xx.xx.xxx: icmp_seq=3 ttl=57 time=6.94 ms

--- xxx.xx.xx.xxx ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 6.777/6.846/6.940/0.096 ms

(额外提一句:windows执行ping时包的大小为32,linux为56)

鉴于linux、win的差异,修改了下getCheckResult方法

private static int getCheckResult(String line) {
        if ((line.contains("ttl=") || line.contains("TTL=")) && line.contains("ms")) {
            return 1;
        }
        return 0;
    }

欢迎大佬们有更优越的方法提出


补充:
可通过执行以下命令(win、linux通用),查看ping动作可带哪些参数

ping -help


————————————————
版权声明:本文为CSDN博主「Asia_rao」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Asia_rao/article/details/129593846

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值