不好意思 不是什么icmp包 就是发送的 packet 呵呵
解决方案 »
- xml selectSingleNode方法linux下报NullPointerException
- 请问个BigDecimal的原因、
- 普元EOS怎么集成jfreechart?
- 有高手通过java读、写vcf通讯录格式吗?
- 请高手前辈分析这个怎么查啊!!
- apache+struts+mysql报错,跪求解决!
- 新手发问:关于一对多的实现问题,请大家帮助!!!
- jBuilder2005+spring2.0.8开发中出现如下异常,请专家门指点迷津
- jasperreport在linux下报错?
- 由Spring引出的关于java泛型的疑问(超难)
- SSH开发上配置mappingResources的时候为什么以下两种方式第二种不好用呢,谢谢
- 接着提问hibernate
你要得到发包收包的情况,可以在
ping statistics 信息里得到
另外你的
int count = 10, index;
//最多只读10行
10行太少了
或则有什么别当然方法?
1. 利用操作系统的 ping 命令,再对输出进行分析。
2. 直接利用 ping 使用的ICMP协议,自己发包分析。
import java.io.InputStreamReader; public class ICMP {
/**
* ping 的一种实现,调用操作系统的ping命令
*/
public static int ping(String host) {
String system = (String) (System.getProperty("os.name")).toLowerCase();
String command = "";
if (system.indexOf("win") != -1) {
command += "ping -n 5 " + host;// 可以设置ping 的次数
} else if (system.indexOf("linux") != -1) {
command += "ping -t 4 " + host; // ping 四次
} else {
command += "ping " + host;
}
int minTime = Integer.MAX_VALUE, curTime;
try {
Process process = Runtime.getRuntime().exec("ping " + host);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
// 最多只读10行
while ((line = in.readLine()) != null ) {
line = line.toLowerCase();
/*
if ((index = line.indexOf("time")) != -1) {
byte[] buf = line.getBytes();
int start = 0, end = buf.length, i, j;
for (i = index + 4; i < buf.length; i++) {
if (Character.isDigit((char) buf[i])) {
start = i;
break;
}
}
if (i == buf.length)
continue;
for (j = start; j < buf.length; j++) {
if (Character.isLetter((char) buf[j])) {
end = j;
break;
}
}
curTime = Integer.parseInt(new String(buf, start, end - start));
if (curTime < minTime) {
minTime = curTime;
}
}
*/
System.out.println(line);
}
} catch (Exception ex) {
return Integer.MAX_VALUE;
}
return minTime;
} public static void main(String args[]) {
ICMP ic = new ICMP();
System.out.println(ic.ping("10.64.2.7"));
} } 这是我改过的 运行后输出的信息为 pinging 10.64.2.7 with 32 bytes of data:
reply from 10.64.2.7: bytes=32 time <1ms ttl=252 reply from 10.64.2.7: bytes=32 time <1ms ttl=252 reply from 10.64.2.7: bytes=32 time <1ms ttl=252 reply from 10.64.2.7: bytes=32 time <1ms ttl=252
ping statistics for 10.64.2.7: packets: sent = 4, received = 4, lost = 0 (0% loss), approximate round trip times in milli-seconds: minimum = 0ms, maximum = 0ms, average = 0ms 请问 大虾门 有没 方法 可以将 直接取得 sent 和received 的 如果是这样的信息话 我想 要费好多事进行判断与截取了
1..请问正则表达式 该怎么写 小弟没怎么接触过
package com; import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.regex.*;public class ICMP {
/**
* ping 的一种实现,调用操作系统的ping命令
*/
public static String ping(String host) {
StringBuffer result = new StringBuffer();
String system = (String) (System.getProperty("os.name")).toLowerCase();
String command = "";
if (system.indexOf("win") != -1) {
command += "ping -n 5 " + host;// 可以设置ping 的次数
} else if (system.indexOf("linux") != -1) {
command += "ping -t 4 " + host; // ping 四次
} else {
command += "ping " + host;
}
Pattern p = Pattern.compile("\\d(?=,)"); //获取send和receive的正则表达式
int curTime;
try {
Process process = Runtime.getRuntime().exec("ping " + host);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
// 最多只读10行
while ((line = in.readLine()) != null ) {
line = line.toLowerCase();
System.out.println(line);
Matcher m = p.matcher(line);
while(m.find())
{
result.append(m.group() + " ");
}
}
} catch (Exception ex) {
System.out.println(ex);
}
return result.toString();
} public static void main(String args[]) {
ICMP ic = new ICMP();
Pattern p = Pattern.compile("\\d+\\s+");
String str = ic.ping("10.64.2.7");
Matcher m = p.matcher(str);
m.find();
System.out.println("send: " + m.group());
m.find();
System.out.println("received: " + m.group());
} }