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服务器 ...
随机推荐
- 关于THINKPHP的addAll支持的最大数据量
Thinkphp中的Model操作有两个方法:add()和addAll $User = M("User"); // 实例化User对象 $data['name'] = 'Think ...
- js取float型小数点后x位数的方法
js中取小数点后两位方法最常用的就是四舍五入函数了,前面我介绍过js中四舍五入一此常用函数,这里正好用上,下面我们一起来看取float型小数点后两位一些方法总结 以下我们将为大家介绍 JavaScri ...
- HeapAlloc 和 GlobalAlloc 以及 VirtualAlloc 三者之间的关系(转)
VirtualAlloc 一次分配 1PAGE 以上的 RAM. 每次分配都是 PAGE 的整数倍. 你不会想为了分配 1 个 BYTE 的空间而浪费剩下的 4095 字节. OK, 你可以自己写算法 ...
- BitTorrent Sync 老版本
Sync version 1.4.111 Installer for Windows: BTSync.exe BTSync_x64.exe Installer for OSX: BTSync.dmgG ...
- 求平方根C++
求平方根,正根.曾经都不会.昨天看数学,看到了,写了出来.自己又小优化了一下,非常不错. // squareRoot.cpp -- 2011-08-29-01.04 #include "st ...
- 用最简单的例子理解对象为Null模式(Null Object Pattern)
所谓的"对象为Null模式",就是要求开发者考虑对象为Null的情况,并设计出在这种情况下的应对方法. 拿"用最简单的例子理解策略模式(Strategy Pattern) ...
- jQuery碎语(1) 基础、选择要操作的元素、处理DOM元素
1.基础 jquery对象集: $():jquery对象集合 获取jquery对象集中的元素: 使用索引获取包装器中的javascript元素:var temp = $('img[alt]')[0] ...
- Windows系统虚拟内存文件和休眠缓存大小优化
虚拟内存的大小设置 虚拟内存的文件 pagefile.sys 一般在系统盘的根目录下,默认情况下会比较大.下面给出缩小设置方式. 我的电脑(鼠标右键)--属性--高级系统设置--切换到“高级”选项卡- ...
- python文本 去掉字符串前后空格
python文本 去掉字符串前后空格 场景: 去掉字符串前后空格 可以使用strip,lstrip,rstrip方法 >>> a="abc".center (30 ...
- SVN环境
SVN服务器搭建和使用(一) Subversion是优秀的版本控制工具,其具体的的优点和详细介绍,这里就不再多说. 首先来下载和搭建SVN服务器. 现在Subversion已经迁移到apache网站上 ...