|
1
|
package test;
|
|
2
|
|
|
3
|
import java.io.PrintStream;
|
|
4
|
import java.net.InetAddress;
|
|
5
|
import java.net.NetworkInterface;
|
|
6
|
import java.util.HexFormat;
|
|
7
|
|
|
8
|
public class TestHost
|
|
9
|
{
|
|
10
|
public static boolean isValidHostNameOrIpAddress(String host)
|
|
11
|
{
|
|
12
|
try
|
|
13
|
{
|
|
14
|
InetAddress addr = InetAddress.getByName(host);
|
|
15
|
if (addr.isLoopbackAddress() || addr.isAnyLocalAddress())
|
|
16
|
{
|
|
17
|
return false;
|
|
18
|
}
|
|
19
|
return NetworkInterface.getByInetAddress(addr) != null;
|
|
20
|
}
|
|
21
|
catch(Throwable e)
|
|
22
|
{
|
|
23
|
return false;
|
|
24
|
}
|
|
25
|
}
|
|
26
|
|
|
27
|
public static void main(String[] args)
|
|
28
|
{
|
|
29
|
int len = args.length;
|
|
30
|
if (len <= 0)
|
|
31
|
{
|
|
32
|
System.out.println("Usage: java -cp . test.TestHost host1 [ host2 ...]");
|
|
33
|
return;
|
|
34
|
}
|
|
35
|
PrintStream ps = new PrintStream(System.out, true);
|
|
36
|
for (int i = 0; i < len; i++)
|
|
37
|
{
|
|
38
|
try
|
|
39
|
{
|
|
40
|
String host = args[i];
|
|
41
|
ps.append("host = ").append(host).append("\r\n");
|
|
42
|
|
|
43
|
boolean valid = isValidHostNameOrIpAddress(host);
|
|
44
|
ps.append("valid = ")
|
|
45
|
.append(String.valueOf(valid))
|
|
46
|
.append("\r\n");
|
|
47
|
|
|
48
|
InetAddress addr = InetAddress.getByName(host);
|
|
49
|
ps.append("addr = ")
|
|
50
|
.append(addr.toString())
|
|
51
|
.append(", loopback=")
|
|
52
|
.append(String.valueOf(addr.isLoopbackAddress()))
|
|
53
|
.append(", wildcard_address=")
|
|
54
|
.append(String.valueOf(addr.isAnyLocalAddress()))
|
|
55
|
.append("\r\n");
|
|
56
|
|
|
57
|
NetworkInterface nic = NetworkInterface.getByInetAddress(addr);
|
|
58
|
ps.append("nic = ")
|
|
59
|
.append(nic.toString())
|
|
60
|
.append(", loopback=")
|
|
61
|
.append(String.valueOf(nic.isLoopback()))
|
|
62
|
.append(", virtual=")
|
|
63
|
.append(String.valueOf(nic.isVirtual()))
|
|
64
|
.append(", up=")
|
|
65
|
.append(String.valueOf(nic.isUp()))
|
|
66
|
.append(", mtu=")
|
|
67
|
.append(String.valueOf(nic.getMTU()))
|
|
68
|
.append(", had=")
|
|
69
|
.append(nic.getHardwareAddress() != null ? HexFormat.of().formatHex(nic.getHardwareAddress()) : null)
|
|
70
|
.append("\r\n");
|
|
71
|
}
|
|
72
|
catch(Throwable e)
|
|
73
|
{
|
|
74
|
e.printStackTrace(ps);
|
|
75
|
}
|
|
76
|
}
|
|
77
|
|
|
78
|
}
|
|
79
|
|
|
80
|
}
|