-
Notifications
You must be signed in to change notification settings - Fork 0
/
Host.java
71 lines (63 loc) · 2.87 KB
/
Host.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.lang.reflect.Field;
import sun.net.InetAddressCachePolicy;
public class Host {
private static enum Input {
count("-loop"), interval("-interval"), host("-hostname");
String name;
Input(String name) {
this.name = name;
}
static Input valueof(String name) {
switch (name) {
case "-loop":
return count;
case "-interval":
return interval;
case "-hostname":
return host;
default:
System.err.println(
String.format("only support input: %s(how many times to lookup the host), %s(waiting between each lookup)", count.name, interval.name));
throw new RuntimeException("faiil to parse the input parameters " + name);
}
}
}
private static Map<Input, String> inputs = new HashMap<>();
public static void main(String[] args) throws Throwable {
// check if ipv6 supported
Field ipv6Impl = InetAddress.class.getDeclaredField("impl");
ipv6Impl.setAccessible(true);
Object obj = ipv6Impl.get(InetAddress.class);
boolean ipv6Support = obj.getClass().getName().equals("java.net.Inet6AddressImpl");
System.out.println(System.getProperty("os.name") + " support " + (ipv6Support ? "ipv6" : "ipv4"));
// modify dns cache time
Field cache = sun.net.InetAddressCachePolicy.class.getDeclaredField("cachePolicy");
cache.setAccessible(true);
System.out.println("before change, dns cache is " + cache.get(sun.net.InetAddressCachePolicy.class) + "s");
cache.set(sun.net.InetAddressCachePolicy.class, 0);
System.out.println("after change, dns cache is " + cache.get(sun.net.InetAddressCachePolicy.class) + "s");
// parse the input
for (int i = 0; args != null && i < args.length; i++) {
inputs.put(Input.valueof(args[i]), args[++i]);
}
String hostname = inputs.get(Input.host) == null ? "baidu.com" : inputs.get(Input.host);
System.out.print("hostname is " + hostname);
int count = inputs.get(Input.count) == null ? 20 : Integer.valueOf(inputs.get(Input.count));
System.out.print("; count loop is " + count);
int interval = inputs.get(Input.interval) == null ? 5 : Integer.valueOf(inputs.get(Input.interval));
System.out.println("; every " + interval + " seconds");
int i = 0;
while (i++ < count) {
System.out.println("----------");
Arrays.asList(InetAddress.getAllByName(hostname)).forEach(System.out::println);
TimeUnit.SECONDS.sleep(interval);
}
}
}