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. Python:format()方法

    转于:https://blog.csdn.net/zhang89xiao/article/details/53818906 博主:张肖的博客 描述: format的格式 replacement_fie ...

  2. 在VirtualBox中安装CentOS 7【转载】

    当初接触Linux的时候,因为条件限制,只能在VirtualBox虚拟机中安装Linux系统使用,由于是小白,爬了好多坑.于是决定写一篇关于在虚拟机中安装linux系统的文章.一是为了巩固自己的知识, ...

  3. 服务器修改密码cmd

    net user  账号  要修改的密码

  4. shell自动收集服务器硬件系统信息

    shell自动收集服务器硬件系统信息,插入数据库并通过web页面显示. 一,shell自动收集服务器硬件系统信息,插入数据库.#centos 7操作系统下 #!/bin/bash #auto get ...

  5. 06_android虚拟机介绍

    分辨率不用选太高,否则会占用太大内存.你选高分辨率一跑起来会干掉你的500多MB的内存.1/8内存就没了.百分之97%或者是98%的设备都是ARM CPU.ARM自己不生产CPU,它生产的是一个标准的 ...

  6. PandaSeq安装报错ltld required, install libtool library

    PandaSeq安装 $ ./autogen.sh && ./configure && make && sudo make install PandaS ...

  7. 2、Spark基本工作原理与RDD

    一.基本工作原理 1.特点 分布式: 主要是基于内存(少数情况基于磁盘): spark与,MapReduce最大的不同在于迭代式计算: MR分为两个阶段,map和reduce,两个阶段完了我们,job ...

  8. es6基础系列五--数组的拓展

    Array.from() 用于将两类对象转为真正的数组,类似数组对象和可遍历对象(包括数据结构Set和Map)转化为数组 格式:Array.from(arrayLike[, mapFn[, thisA ...

  9. jquery插件-自由拖拽

    最近工作不是很忙,学习之余想整理一些代码出来,首先想到的就是是js拖拽. 两年前去某公司面试的时候,曾经被问过这个问题,如何在页面上拖放元素,尽管现在看起来很简单,但当时的我半点思路都没有,面试想当然 ...

  10. FMDB 使用注意点

    关于FMDB最基本的使用我们就不在说了,这个网上大把的文章介绍,我就在这里总结几点我最近在写一个小东西的时候注意到的一点点东西: 一: 怎么看真机上SQLite数据库 我们在开发的过程中肯定有使用到真 ...