Pinger2
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;
// 开始时间
private String startTime;
// 结束时间
private String endTime;
/**
* 构造函数
* @param ip
* @param timesNum
*/
public Pinger(String ip,int timesNum){
this.ip=ip;
this.timesNum=timesNum;
}
/**
* Ping后计算响应的个数
* @return
* @throws Exception
*/
public int countReply() throws Exception{
String destIp=ip;
int maxCount=timesNum;
LineNumberReader input = null;
try {
startTime=DateTimeUtil.getCurrDateTime();
// 根据操作系统组合命令
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);
} else if (osName.startsWith("Linux")) {
return parseLinuxMsg(reponse, 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());
}
}
endTime=DateTimeUtil.getCurrDateTime();
}
return 0;
}
/**
* ping次数和响应数相等算ping通
* @return
* @throws Exception
*/
public boolean isPass() throws Exception{
return countReply()==timesNum;
}
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 String getStartTime() {
return startTime;
}
public String getEndTime() {
return endTime;
}
public static void main(String[] args) throws Exception{
Pinger p=new Pinger("www.163.com",5);
System.out.println(p.countReply());
System.out.println(p.getStartTime());
System.out.println(p.getEndTime());
}
}
Pinger2的更多相关文章
- Kali学习笔记7:三层发现
三层发现:发送ICMP/IP数据包探测 第一种方式: 就是很简单的Ping命令: 不过linux的ping命令和windows的ping命令不一样,它会默认不停止地发数据包 我们可以通过-c参数来设置 ...
- 【python】列出http://www.cnblogs.com/xiandedanteng中所有博文的标题
代码: # 列出http://www.cnblogs.com/xiandedanteng中所有博文的标题 from bs4 import BeautifulSoup import requests u ...
- Node.js 网页爬虫再进阶,cheerio助力
任务还是读取博文标题. 读取app2.js // 内置http模块,提供了http服务器和客户端功能 var http=require("http"); // cheerio模块, ...
- Node.js 网页瘸腿稍强点爬虫再体验
这回爬虫走得好点了,每次正常读取文章数目总是一样的,但是有程序僵住了情况,不知什么原因. 代码如下: // 内置http模块,提供了http服务器和客户端功能 var http=require(&qu ...
- Node.js 网页瘸腿爬虫初体验
延续上一篇,想把自己博客的文档标题利用Node.js的request全提取出来,于是有了下面的初哥爬虫,水平有限,这只爬虫目前还有点瘸腿,请看官你指正了. // 内置http模块,提供了http服务器 ...
随机推荐
- DP Training(Updating)
感觉前面做了那么多$dp$全是自己想的还是太少啊…… 好像在LZT的博客上看到了不错的资源?赶紧开坑,以一句话题解为主 Codeforces 419B 第一题就开始盗图 由于只有一个交点,手玩一下发现 ...
- BZOJ 1528 [POI2005]sam-Toy Cars(优先队列)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1528 [题目大意] 地上最多可以放k个玩具,现在给出需求顺序, 问最少需要去架子上拿几 ...
- Ajax 的概念及过程?Ajax 的交互模型?同步和异步的区别?如何解决跨域问题?
Ajax 是什么: 1) 通过异步模式,提升了用户体验 2) 优化了浏览器和服务器之间的传输,减少不必要的数据往返,减少了带宽占用 3) Ajax 在客户端运行,承担了一部分本来由服务器承担的工 ...
- Topcoder SRM 643 Div1 250<peter_pan>
Topcoder SRM 643 Div1 250 Problem 给一个整数N,再给一个vector<long long>v; N可以表示成若干个素数的乘积,N=p0*p1*p2*... ...
- C# 高级编程9 第30章MEF C#可扩展编程之MEF第2章(抄录)
Managed Extensibility Framework (MEF) 什么是 MEF? Managed Extensibility Framework 即 MEF 是用于创建轻量.可扩展应用 ...
- angularjs鼠标移入移出实现显示隐藏
<tr ng-repeat="item in items track by $index"> <td data-title="操作" alig ...
- JS简单实现二级联动菜单
<form method="post" action=""> 省/市:<select id="province" onch ...
- 力特ZE398C驱动光盘-USB转RS232-支持Windows 10/Mac
这个工具是USB1.1的,相对来说比较老,一开始做小白鼠不知道买了USB1.1的,所以我不建议买这个,还有其它的型号,支持USB2.0和USB3.0,不过价格也相对来说比较贵,这个才30块钱左右. 关 ...
- CentOS 7修改网卡名为eth0
第一步: 编辑文件加入如下所示参数 vi /etc/sysconfig/grub GRUB_CMDLINE_LINUX=”rd.lvm.lv=vg0/swap vconsole.keymap=us c ...
- IOS7开发~Images.xcassets
from:http://blog.csdn.net/liufan321/article/details/9121241 新建项目,如下所示: 本文分享一下Images.xcassets的体验~_~ 1 ...