Pinger
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
public class Pinger{
// IP地址
private String ip;
// Ping次数
private int timesNum;
/**
* 构造函数
* @param ip
* @param timesNum
*/
public Pinger(String ip,int timesNum){
this.ip=ip;
this.timesNum=timesNum;
}
/**
* ping次数和响应数相等算ping通
* @return
* @throws Exception
*/
public boolean isPass() throws Exception{
String destIp=ip;
int maxCount=timesNum;
LineNumberReader input = null;
try {
// 根据操作系统组合命令
String osName = System.getProperties().getProperty("os.name");
String pingCmd = null;
if (osName.startsWith("Windows")) {
pingCmd = "cmd /c ping -n {0} {1}";
pingCmd = MessageFormat.format(pingCmd, maxCount, destIp);
} else if (osName.startsWith("Linux")) {
pingCmd = "ping -c {0} {1}";
pingCmd = MessageFormat.format(pingCmd, maxCount, destIp);
} else {
throw new Exception("not support OS");
}
// ping完得到响应
Process process = Runtime.getRuntime().exec(pingCmd);
InputStreamReader ir = new InputStreamReader(process
.getInputStream());
input = new LineNumberReader(ir);
String line;
List<String> reponse = new ArrayList<String>();
while ((line = input.readLine()) != null) {
if (!"".equals(line)) {
reponse.add(line);
// System.out.println("====:" + line);
}
}
// 分析响应
if (osName.startsWith("Windows")) {
return parseWindowsMsg(reponse, maxCount)==maxCount;
} else if (osName.startsWith("Linux")) {
return parseLinuxMsg(reponse, maxCount)==maxCount;
}
} catch (IOException e) {
System.out.println("IOException " + e.getMessage());
} finally {
if (null != input) {
try {
input.close();
} catch (IOException ex) {
System.out.println("close error:" + ex.getMessage());
}
}
}
return false;
}
private int parseWindowsMsg(List<String> reponse, int total) {
int countTrue = 0;
int countFalse = 0;
for (String str : reponse) {
if (str.startsWith("来自") || str.startsWith("Reply from")) {
countTrue++;
}
if (str.startsWith("请求超时") || str.startsWith("Request timed out")) {
countFalse++;
}
}
return countTrue;
}
private int parseLinuxMsg(List<String> reponse, int total) {
int countTrue = 0;
for (String str : reponse) {
if (str.contains("bytes from") && str.contains("icmp_seq=")) {
countTrue++;
}
}
return countTrue;
}
public static void main(String[] args) throws Exception{
Pinger p=new Pinger("123.abc.xyz.com",5);
System.out.println(p.isPass());
}
}
Pinger的更多相关文章
- Assignment 2: UDP Pinger[课后作业]
Computer Networking : A Top-Down Approach 的课后作业. 要求: 基于UDP协议,实现一个Pinger工具. 服务端代码已经提供了,自己实现客户端的代码. 完整 ...
- Zabbix icmp pinger processes more than 75% busy
Zabbix icmp pinger processes more than 75% busy Zabbix server报"Zabbix icmp pinger processes m ...
- MongoDB使用小结:一些不常见的经验分享
最近一年忙碌于数据处理相关的工作,跟MongoDB打交道极多,以下为实践过程中的Q&A,后续会不定期更新补充. 另有<MongoDB使用小结:一些常用操作分享>,注:本文完成时Mo ...
- iOS开发中获取WiFi相关信息
iOS 开发中难免会遇到很多与网络方面的判断,这里做个汇总,大多可能是与WiFi相关的. 1.Ping域名.Ping某IP 有 时候可能会遇到ping 某个域名或者ip通不通,再做下一步操作.这里的p ...
- WordPress基础:让搜索引擎及时更新文章
如果文章更新之后,想让搜索引擎也及时更新,你需要以下步骤 1.快速编辑文章时,勾选ping 2.设置->阅读,保证搜索引擎允许搜索 3.设置->撰写->添加url 通知url列表参考 ...
- squid源码安装下的conf文件默认值和提示
# WELCOME TO SQUID 3.0.STABLE26# ----------------------------## This is the default Squid c ...
- iOS Wi-Fi
查漏补缺集是自己曾经做过相关的功能,但是重做相关功能或者重新看到相关功能的实现,感觉理解上更深刻.这一类的文章集中记录在查漏补缺集. iOS 开发中难免会遇到很多与网络方面的判断,这里做个汇总,大多可 ...
- Fping
(十大特色功能) Ping是最常用的网络测试工具,ping的测试功能其实比较多,xp系统的ping有12个选项.但是,fping测试工具有25个选项,在ping的基础上增加了许多专业的功能,可用于更深 ...
- squid 学习笔记
Squid学习笔记 1.安装前的配置 编译安装之前需要校正的参数主要包括File Descriptor和Mbuf Clusters. 1.File Descriptor 查看文件描述符的限制数目: u ...
随机推荐
- BZOJ 3572: [Hnoi2014]世界树 虚树 树形dp
https://www.lydsy.com/JudgeOnline/problem.php?id=3572 http://hzwer.com/6804.html 写的时候参考了hzwer的代码,不会写 ...
- BZOJ 4605 崂山白花蛇草水(权值线段树+KD树)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4605 [题目大意] 操作 1 x y k 表示在点(x,y)上放置k个物品, 操作 2 ...
- Beego 和 Bee 的开发实例
Beego不是一般的web开发包.它构建在大量已存在的Go之上,提供了许多的功能,以下是提供的功能: 一个完整的ORM 缓存 支持session 国际化(i18n) 实时监测和重载 发布支持 ==== ...
- WEB架构师成长之路 三
Web架构师究竟都要学些什么?具备哪些能力呢?先网上查查架构师的大概的定义,参见架构师修炼之道这篇文章,写的还不错,再查查公司招聘Web架构师的要求. 总结起来大概有下面几点技能要求: 一. 架构师有 ...
- Html的学习随笔
在<head>的<style>中定义样式,有#号,比如#header就是定义一种名为header的样式,后面用id=header来调用:而无#号,比如直接就是header,那后 ...
- word-ladder总结
title: word ladder总结 categories: LeetCode tags: 算法 LeetCode comments: true date: 2016-10-16 09:42:30 ...
- linux 文件处理大杂烩
1.对文件某行进行统计排序 awk '{ printf "%-40s \n",$4}' /var/log/yum.log | sort | uniq -c | sort -nk ...
- PostgreSQL 资料库
https://yq.aliyun.com/articles/59251 https://github.com/digoal/blog/blob/master/201609/20160929_02.m ...
- jQuery制作水平多级下拉菜单
本篇体验使用jQuery制作水平的.多级的.下拉菜单. 下拉菜单的html部分如下. <body> <nav class="main-nav"> <u ...
- NodeJS学习资料合集
1. 官网 nodejs 2. How do I get started with Node.js,stackoverflow提问,收集非常多实用的网站 3. node-books.github收 ...