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服务器 ...
随机推荐
- LCT维护子树信息
有些题目,在要求支持link-cut之外,还会在线询问某个子树的信息.LCT可以通过维护虚边信息完成这个操作. 对于LCT上每个节点,维护两个两sz和si,后者维护该点所有虚儿子的信息,前者维护该点的 ...
- bzoj 2300 动态维护上凸壳(不支持删除)
新技能GET. 用set保存点,然后只需要找前趋和后继就可以动态维护了. /************************************************************** ...
- redis学习之一 - linux下安装配置
Content 0.序 1.如何安装? 2.配置参数及其意义 3.设为linux服务 0.序 本文主要是记录Redis在 Centos下的安装配置 .文中如无特别说明.表示redis-3.2.10代码 ...
- mybatis_mybatis写mapper文件注意事项
xml中某些特殊符号作为内容信息时需要做转义,否则会对文件的合法性和使用造成影响 < < > > & & ' ' " " ...
- POJ 1330 Nearest Common Ancestors (LCA,dfs+ST在线算法)
Nearest Common Ancestors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14902 Accept ...
- sunstudio 12.3 on solaris 10
#include <stdio.h> #include <stdlib.h> #define RPT 10000000 int main() { int i=1; for( i ...
- C++空类产生哪些成员函数 || C++类可以自动生成的6个成员函数
class Empty { public: Empty(); // 缺省构造函数 Empty( const Empty& ); // 拷贝构造函数 ~Empty ...
- c#开发地磅称重软件
2012年时即做过一个地磅称重软件,最近公司又接了一个地磅过磅软件的项目,把遇到的问题总结一下以备后用. 1.接线问题 因为客户方原来单独使用仪表,仪表未有接线和电脑连接,为此颇费周折才做好了接线.接 ...
- 经典数独游戏+数独求解器—纯C语言实现
"心常乐数独小游戏"(下面简称"本软件")是一款windows平台下的数独游戏软件. 本软件是开源.免费软件. 本软件使用纯C语言编写,MinGW编译,NSIS ...
- iOS:制作一个简易的计算器
初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉. // // ViewController.m // 计算器 // // Created ...