22:04:08.002 [main] INFO  Ping - 目标地址   192.168.1.107 是否可到达:true
22:04:08.006 [main] INFO Ping - 执行命令 ping 192.168.1.107 -n 4 -w 5000
22:04:11.085 [main] INFO Ping - 目标地址 192.168.1.107 是否可PING通:true
22:04:11.140 [main] INFO Ping -
22:04:11.140 [main] INFO Ping - 正在 Ping 192.168.1.107 具有 32 字节的数据:
22:04:11.141 [main] INFO Ping - 来自 192.168.1.107 的回复: 字节=32 时间<1ms TTL=64
22:04:12.141 [main] INFO Ping - 来自 192.168.1.107 的回复: 字节=32 时间<1ms TTL=64
22:04:13.142 [main] INFO Ping - 来自 192.168.1.107 的回复: 字节=32 时间<1ms TTL=64
22:04:14.144 [main] INFO Ping - 来自 192.168.1.107 的回复: 字节=32 时间<1ms TTL=64
22:04:14.144 [main] INFO Ping -
22:04:14.144 [main] INFO Ping - 192.168.1.107 的 Ping 统计信息:
22:04:14.144 [main] INFO Ping - 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
22:04:14.144 [main] INFO Ping - 往返行程的估计时间(以毫秒为单位):
22:04:14.144 [main] INFO Ping - 最短 = 0ms,最长 = 0ms,平均 = 0ms
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; import org.apache.log4j.Logger;
import org.junit.Test; public class Ping { private static final Logger LOGGER = Logger.getLogger(Ping.class); @Test
public void scanPortTest() { scanPort("192.168.1.107", 9200); // for (int i = 1000; i <= 9999; i++) {
// scanPort("127.0.0.1", i);
// }
} public void scanPort(String ipAddress, int port) {
Socket client = null;
try {
client = new Socket();
client.connect(new InetSocketAddress(ipAddress, port), 300);
LOGGER.info(ipAddress + ":" +port);
} catch (IOException e) {
LOGGER.error("端口:" + port + " 异常 " + e);
} finally {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} @Test
public void discoveryTest() throws Exception {
Discovery("192.168.1", 1, 254);
} private static List<String> Discovery(String networkSection, int start, int end) throws Exception {
for (int i = start; i <= end; i++) {
String ipAddress = networkSection + "." + i;
ping(ipAddress);
}
return null;
} private static boolean ping(String ipAddress) throws Exception {
// 超时应该在3钞以上
int timeOut = 3000;
// 当返回值是true时,说明host是可用的,false则不可。
boolean status = InetAddress.getByName(ipAddress).isReachable(timeOut);
LOGGER.info("目标地址 " + ipAddress + " 是否可到达:" + status);
return status;
} private static void ping02(String ipAddress) throws Exception {
String line = null;
try {
Process pro = Runtime.getRuntime().exec("ping " + ipAddress);
InputStreamReader isr = new InputStreamReader(pro.getInputStream(), "GBK");
BufferedReader buf = new BufferedReader(isr);
while ((line = buf.readLine()) != null)
LOGGER.info(line);
} catch (Exception ex) {
LOGGER.error(ex.getMessage());
}
} private static boolean ping(String ipAddress, int pingTimes, int timeOut) {
BufferedReader in = null;
// 将要执行的ping命令,此命令是windows格式的命令
Runtime r = Runtime.getRuntime();
String pingCommand = "ping " + ipAddress + " -n " + pingTimes + " -w " + timeOut;
try { // 执行命令并获取输出
LOGGER.info("执行命令 " + pingCommand);
Process p = r.exec(pingCommand);
if (p == null) {
return false;
}
in = new BufferedReader(new InputStreamReader(p.getInputStream()));
int connectedCount = 0;
String line = null;
while ((line = in.readLine()) != null) {
connectedCount += getCheckResult(line);
} // 如果出现类似=23ms TTL=62这样的字样,出现的次数=测试次数则返回真
boolean flag = connectedCount == pingTimes;
LOGGER.info("目标地址 " + ipAddress + " 是否可PING通:" + flag);
return flag;
} catch (Exception ex) {
LOGGER.error(ex);
return false;
} finally {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} // 若line含有=18ms TTL=16字样,说明已经ping通,返回1,否則返回0.
private static int getCheckResult(String line) {
Pattern pattern = Pattern.compile("(\\d+ms)(\\s+)(TTL=\\d+)", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(line);
while (matcher.find()) {
return 1;
}
return 0;
} @Test
public void pingTest() throws Exception {
String ipAddress = "192.168.1.107";
ping(ipAddress);
ping(ipAddress, 4, 5000);
ping02(ipAddress);
}
}

最新文章

  1. jQuery对话框插件 ThickBox
  2. 使用MbrFix.exe修复MBR分区表
  3. UVa 1363 (数论 数列求和) Joseph&#39;s Problem
  4. 【socket.io研究】1.官网的一些相关说明,概述
  5. 源码实现 --&gt; strdel
  6. Vue.js 2.x笔记:指令(4)
  7. 【Mac】【问题】
  8. UVA11400-Lighting System Design(动态规划基础)
  9. Matplotlib学习---用matplotlib画热图(heatmap)
  10. Spring的aop操作
  11. Ruby面向对象
  12. java mqtt
  13. 零基础快速入门web学习路线(含视频教程)
  14. python基础09_字符串格式化
  15. numpy中 array数组的shape属性
  16. cmd--命令短集
  17. Log4j读取配置文件并使用
  18. ASP.NET:邮件服务器与客户端
  19. java_Proxy动态代理_AOP
  20. 转 Linux会话浅析(写得极好,表述清楚语言不硬)

热门文章

  1. Amaze UI 框架
  2. 低调的css3属性font-size-adjust
  3. Linux keepalived与lvs的深入分析
  4. 问题:MSChart.exe;结果:微软图表控件MsChart使用方法及各种插件下载地址
  5. python变量、类型、运算、输出
  6. Material使用09 MdCheckboxModule、MdMenuModule、MdTooltipModule
  7. Learning Python 010 函数 2
  8. VS编译器中设置 输出窗口 只显示error,不显示warning 要如何配置
  9. 生物数据库介绍——NCBI
  10. 死磕 java同步系列之开篇