java net ping_Java里Ping DNS的多种方式汇总

Java里Ping DNS的多种方式汇总,不多说直接代码。

package com.jvwl.rds.test;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.net.Inet4Address;

import java.net.Inet6Address;

import java.net.InetAddress;

import java.net.InetSocketAddress;

import java.net.Socket;

import java.util.Arrays;

import java.util.List;

import org.testng.annotations.Test;

import lombok.extern.slf4j.Slf4j;

/**

* Created by jervalj on 2018-09-12

*/

@Slf4j

public class PingDnsAndPort {

@Test

public void PingDnsWithCommand() {

String dns = "www.hao123.com";

Runtime runtime = Runtime.getRuntime();

BufferedReader bufferedReader = null;

boolean result = false;

try {

String command = "ping " + dns;

log.info("CMD: {}", command);

Process process = runtime.exec(command);

bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));

String line = null;

while (null != (line = bufferedReader.readLine())) {

if (!result && line.contains("TTL")) {

result = true;

}

log.info(line);

}

if (result) {

log.info("Ping {} successful!", dns);

} else {

log.info("Ping {} failed!", dns);

}

} catch (IOException e) {

e.printStackTrace();

} finally {

if (null != bufferedReader) {

try {

bufferedReader.close();

} catch (IOException e) {

}

}

}

log.info("---Done----");

}

@Test

public void PingDnsWithJavaApi() {

String dns = "www.hao123.com";

try {

InetAddress inetAddress = InetAddress.getByName(dns);

if (inetAddress instanceof Inet4Address) {

log.info("{} is ipv4", dns);

} else if (inetAddress instanceof Inet6Address) {

log.info("{} is ipv6", dns);

} else {

log.info("{} is unknown type", dns);

}

log.info("HostName:{}", inetAddress.getHostName());

log.info("HostAddress:{}", inetAddress.getHostAddress());

int timeout = 6000;

log.info("InetAddress isReachable:{}", inetAddress.isReachable(timeout));

log.info("Full InetAddress:{}", inetAddress.toString());

} catch (Exception e) {

e.printStackTrace();

}

}

@Test

public void PingDnsAndPortWithSocket() {

String dns = "fca-vm-hc-prod-jb6-rds-g1i1.hyvesolutions.org";

int port = 4447;

Socket socket = new Socket();

InetSocketAddress inetSocketAddress = new InetSocketAddress(dns, port);

boolean result = false;

try {

socket.connect(inetSocketAddress);

result = true;

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

socket.close();

} catch (IOException e) {

}

}

if (result) {

log.info("Ping {} successful!", dns);

} else {

log.info("Ping {} failed!", dns);

}

}

@Test

public void PingDnsAndPortWithSocketByBatch() {

List dnsList = Arrays.asList("fca-vm-uat-jboss6-rds-g1i1", "ton-vm-uat-jboss6-rds-g1i8", "ton-vm-prod-jboss6-rds-g1i1");

int port = 4447;

for (String dns:dnsList) {

Socket socket = new Socket();

InetSocketAddress inetSocketAddress = new InetSocketAddress(dns, port);

boolean result = false;

try {

socket.connect(inetSocketAddress);

result = true;

} catch (IOException e) {

} finally {

try {

socket.close();

} catch (IOException e) {

}

}

if (result) {

log.info("Ping {} successful!", dns);

} else {

log.info("Ping {} failed!", dns);

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值