Java ping 主机 端口
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 主机 端口的更多相关文章
- Java 实现 telnet命令 验证主机端口的连通性
Java 实现 telnet命令 验证主机端口的连通性 1.Telnet 命令 Telnet协议是TCP/IP协议族中的一员,是Internet远程登录服务的标准协议和主要方式.它为用户提供了在本地计 ...
- 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不通外网 ...
- NAT 模式下虚拟机安装的centos7 ping主机显示connect: Network is unreachable
在虚拟机下安装的centos7使用的网络是NAT模式,安装成功后ping主机地址显示 Network is unreachable 解决方案: 1)使用ifconfig命令查看网卡信息 2)进入/et ...
- bug7 java.net.BindException: 端口被占用。
java.net.BindException: 端口被占用. java.net.BindException: 端口被占用.
- 通过ping 主机名,或者主机名对应的IP地址
通过ping 主机名,或者主机名对应的IP地址: 如下图: 懵了吧? 但是你用 ping 主机名 -4
- 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 ...
- 判断所ping主机的操作系统
根据它的值判断所ping主机的操作系统类型. TTL被称为生存期,也就是你所传输的数据在网络上经过的路由器的最大个数. 操作系统 TTLLINUX 64WIN2K/NT 128WINDOWS 系列 3 ...
- VMware桥接模式下虚拟机ping主机不通
现象: VMware设置为桥接模式,虚拟机ping主机不通,主机ping虚拟机通. 解决: 尝试以下几种方法 关闭主机(专用网络.来宾或公用网络)和虚拟机的防火墙. 更改桥接的物理网卡,确保是主机正在 ...
- 主机ping虚拟机失败。虚拟机ping主机,可以ping通。
原文:https://blog.csdn.net/ww1473345713/article/details/51490525 今天打开虚拟机,然后用Xshell远程连接,发现连接不上.按照以下顺序检查 ...
随机推荐
- kubeadm 搭建 K8S集群
kubeadm是K8s官方推荐的快速搭建K8s集群的方法. 环境: Ubuntu 16.04 1 安装docker Install Docker from Ubuntu’s repositories: ...
- HTTP 2 VS HTTP 1.1
提升H5应用加载速度的方式有很多,比如缓存.cdn加速.代码压缩合并和图片压缩等技术. 今天介绍的是HTTP 2.0
- mariadb复制
实验环境:CentOS7 #安装mariadb-server #主数据库: [root@~ localhost]#vim /etc/my.cnf.d/server.cnf [mysqld] skip_ ...
- C#设计模式(7)——适配器模式
一.概述 将一个类的接口转换成客户希望的另外一个接口.Adapter模式使得原本由于接口不兼容而不能一起工作的那些类可以在一起工作. 二.模型 三.代码实现 using System; /// 这里以 ...
- 12.Redis Select 命令 - 切换到指定的数据库
转自:http://www.runoob.com/redis/redis-tutorial.html Redis Select 命令用于切换到指定的数据库,数据库索引号 index 用数字值指定,以 ...
- service的生命周期以及两种service的差异
可以看到,两种service的生命周期都相对简单,有一点不同的是,Intentservice每次调用的时候都执行onstartcommand,而boundservice一旦启动了之后,就不会每次执行o ...
- Spring pom配置详解(转)
转载至http://blog.csdn.net/ithomer/article/details/9332071# 原博主注释的很详细 <project xmlns="http://ma ...
- Entity Framework Code-First(7):Inheritance Strategy
Inheritance Strategy in Code-First: We have seen in the Code First Conventions section that it creat ...
- hdu1080
#include<iostream> using namespace std; char s1[105],s2[105]; int val[5][5]={ {5,-1,-2,-1,-3}, ...
- linux 的有用的网站
从windows下移到linux下还有很长的路走阿,慢慢记录一些有用的网站吧 http://www.yolinux.com/ http://linux.die.net/