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);
}
}

Java ping 主机 端口的更多相关文章

  1. Java 实现 telnet命令 验证主机端口的连通性

    Java 实现 telnet命令 验证主机端口的连通性 1.Telnet 命令 Telnet协议是TCP/IP协议族中的一员,是Internet远程登录服务的标准协议和主要方式.它为用户提供了在本地计 ...

  2. From 192.168.25.133 icmp_seq=238 Destination Host Unreachable 虚拟机ping主机不通

    From 192.168.25.133 icmp_seq=238 Destination Host Unreachable 虚拟机ping主机不通,但是主机可以ping通虚拟机,虚拟机ping不通外网 ...

  3. NAT 模式下虚拟机安装的centos7 ping主机显示connect: Network is unreachable

    在虚拟机下安装的centos7使用的网络是NAT模式,安装成功后ping主机地址显示 Network is unreachable 解决方案: 1)使用ifconfig命令查看网卡信息 2)进入/et ...

  4. bug7 java.net.BindException: 端口被占用。

    java.net.BindException:   端口被占用. java.net.BindException:   端口被占用.

  5. 通过ping 主机名,或者主机名对应的IP地址

    通过ping 主机名,或者主机名对应的IP地址: 如下图: 懵了吧? 但是你用 ping 主机名 -4

  6. Linux 能PING IP 但不能PING 主机域名的解决方法 vim /etc/nsswitch.conf hosts: files dns wins

    Linux 能PING IP 但不能PING 主机域名的解决方法 转载 2013年12月25日 10:24:27 13749 . vi /etc/nsswitch.conf hosts: files ...

  7. 判断所ping主机的操作系统

    根据它的值判断所ping主机的操作系统类型. TTL被称为生存期,也就是你所传输的数据在网络上经过的路由器的最大个数. 操作系统 TTLLINUX 64WIN2K/NT 128WINDOWS 系列 3 ...

  8. VMware桥接模式下虚拟机ping主机不通

    现象: VMware设置为桥接模式,虚拟机ping主机不通,主机ping虚拟机通. 解决: 尝试以下几种方法 关闭主机(专用网络.来宾或公用网络)和虚拟机的防火墙. 更改桥接的物理网卡,确保是主机正在 ...

  9. 主机ping虚拟机失败。虚拟机ping主机,可以ping通。

    原文:https://blog.csdn.net/ww1473345713/article/details/51490525 今天打开虚拟机,然后用Xshell远程连接,发现连接不上.按照以下顺序检查 ...

随机推荐

  1. Poj 2662,2909 Goldbach's Conjecture (素数判定)

    一.Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard ...

  2. Scala总结

    Scala总结 ===概述 scala是一门以Java虚拟机(JVM)为目标运行环境并将面向对象和函数式编程的最佳特性结合在一起的静态类型编程语言. scala是纯粹的面向对象的语言.java虽然是面 ...

  3. 一. kafka 入门

    一.基本概念 Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据.这种动作(网页浏览,搜索和其他用户的行动)是在现代网络上的许多社会功能的一个关键因素. K ...

  4. Struts学习总结 学习

    ContextMap 包含值栈包含 root(list结构)和context(map结构)  值栈包含contextMap的引用.  Actioncontext是工具类 可以获取他们 Struts2拥 ...

  5. IFrame与window对象(contentWindow)

    ref:http://blog.csdn.net/dongzhiquan/article/details/5851201 var detialIframe=document.all("det ...

  6. C#String.Split (string[], StringSplitOptions)中的StringSplitOptions是什么意思,看了msdn还是不懂?

    MSDN上面这样子写的: [ComVisibleAttribute(false)] public string[] Split(string[] separator,StringSplitOption ...

  7. 利用StoryBoard编写UITabelViewCell

    举一个炒鸡简单的例子: - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPa ...

  8. Vue 全家桶单元测试简要指南

    此文已由作者张汉锐授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. vue 的单元测试环境 按照目前全家桶的情况,是需要 webpack 的支持的.可以直接使用 vue-cli ...

  9. InnoDB信息说明

    InnoDB是MySQL数据库发展至今一款至关重要的数据库存储引擎,其不仅支持事务特性,并且具有丰富的统计信息,便于数据库管理人员了解最近InnoDB存储引擎的运行状态. 早期版本的InnoDB存储引 ...

  10. GoWeb开发_Iris框架讲解(三):路由功能处理方式

    Context概念 Context是iris框架中的一个路由上下文对象,在iris框架中的源码路径定义为:{$goPath}\github.com\kataras\iris\context\conte ...