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