pingUtil
package unit; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.util.regex.Matcher;
import java.util.regex.Pattern; public class pingUtil {
public static boolean ping(String ipAddress) throws Exception{
int timeOut=3000;
boolean status=InetAddress.getByName(ipAddress).isReachable(timeOut); // 当返回值是true时,说明host是可用的
return status;
} public static void ping02(String ipAddress){
String line=null; try {
Process pro=Runtime.getRuntime().exec("ping "+ipAddress);
BufferedReader buf=new BufferedReader(new InputStreamReader(pro.getInputStream(),"utf-8"));
while((line=buf.readLine())!=null){
System.out.println(line);
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
} public static boolean ping(String ipAddress,int pingTimes,int timeOut){
BufferedReader in=null;
Runtime r= Runtime.getRuntime();
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(),"utf-8"));
int connectedCount=0;
String line=null;
while((line=in.readLine())!=null){
connectedCount += getCheckResult(line);
}//如果出现类似=20ms TTL=22这样的字符,出现的次数=测试次数 则返回真
return connectedCount == pingTimes;
} catch (Exception e) {
e.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){
Pattern pattern=Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while(matcher.find()){
return 1;
}
return 0;
} public static void main(String[] args) throws Exception{
String ipAddress="www.qq.com";
// System.out.println(ping(ipAddress));
ping02(ipAddress);
System.out.println(ping(ipAddress,5,5000));
} }
pingUtil的更多相关文章
- PingUtil in Android
Ping a host in Android:“ping -c 1 127.0.0.1”-c 1: The ping times. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...
- 我的小工具开源一下-PingTest
v博客前言 先交代下背景,最近我们项目组的网络真是太渣了,时常remote不了另外一个地方的机器,过个几分钟就断开连接,太烦躁了,严重影响工作心情...于是想着做个工具记录下每天的断开remote连接 ...
- Windows和Linux下 Java开发ping工具类
package com.test.util; import java.io.BufferedReader; import java.io.IOException; import java.io.Inp ...
- Java 实现判断 主机是否能 ping 通
Java 实现判断 主机是否能 ping 通 代码实现如下: import java.io.IOException; import java.net.InetAddress; import java. ...
随机推荐
- Luogu 4784 [BalticOI 2016 Day2]城市
斯坦纳树复习,我暑假的时候好像写过[JLOI2015]管道连接来着. 设$f_{i, s}$表示以$i$为根,$k$个重要点的连通状态为$s$,($0$代表没有连进最小生成树里面去,$1$代表连进了最 ...
- Luogu 2403 [SDOI2010]所驼门王的宝藏
BZOJ 1924 内存要算准,我MLE了两次. 建立$n + r + c$个点,对于一个点$i$的坐标为$(x, y)$,连边$(n + x, i)$和$(n + r + y, i)$,代表这一列和 ...
- Shiro——认证
引入shiro依赖 <!-- shiro --> <dependency> <!-- shiro-core Required in all environments. - ...
- Python基础-3
目录 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 知识插入:嵌套函数 4.递归 5.匿名函数 6.函数式编程介绍 7.高阶函数 8.内置函数 一.函数基本语法 函数是什么? 函数一词 ...
- (转)Asp.Net生命周期系列一
原文地址:http://www.cnblogs.com/skm-blog/archive/2013/07/07/3176713.html Asp.Net生命周期对于初级甚至中级程序员来说,一直都是一个 ...
- [Lua快速了解一下]Lua运行
-Lua的Hello World print("Hello World") 分号可选 -类似python,进入Lua后再shell中打命令执行语句也可 > print(&qu ...
- cmake安装方法
由于Ubuntu14.04的cmake版本为2.8.x,而如果需要cmake3.x版本时,无法生成makefile,有两种方法可以安装cmake3.10.0: 方法1: sudo apt-get in ...
- Oracle 写存储过程的一个模板还有一些基本的知识点
我很少用Oracle,也算新手,不过其实入手没有那么难,下面只是一个基本知识,高手绕道,其实数据库基本是相同的,这里提供都是基本知识点 有一个Oracle溢出的问题,容易让新手怀疑到无所怀疑,其实就是 ...
- WinForm中自定义搜索框(水印、清空按钮、加载中图标)
public partial class CustomSearchBar : TextBox { private readonly Label lblwaterText = new Label(); ...
- ubuntu 16.4安装toolsbelt heroku
https://devcenter.heroku.com/articles/getting-started-with-python#set-up # Run this from your termin ...