Java 调用window下的ping命令实现ping的函数封装

来源:互联网 发布:http测试工具 windows 编辑:程序博客网 时间:2024/04/30 10:24
/** * 能否ping通IP地址 * @param server IP地址 * @param timeout 超时时长 * @return true能ping通 */public static boolean pingServer(String server, int timeout) {        BufferedReader in = null;        Runtime r = Runtime.getRuntime();        String pingCommand = "ping " + server + " -n 1 -w " + timeout;        try {            Process p = r.exec(pingCommand);            if (p == null) {                return false;            }            in = new BufferedReader(new InputStreamReader(p.getInputStream()));            String line = null;            while ((line = in.readLine()) != null) {                if (line.startsWith("Reply from")||line.contains("TTL")) {                    return true;                }            }        } catch (Exception ex) {            ex.printStackTrace();            return false;        } finally {            try {                in.close();            } catch (IOException e) {                e.printStackTrace();            }        }        return false;    }

实现java ping命令  当然网上有许多方法,但是我觉着这种简单

还有一种是用黑客的洪水攻击的方法

伪造一个ip数据包进行测试

调用的jpcap外部库

正在研究。、。、、、

原创粉丝点击