ping和tracert命令
ping命令一般用来测试两台机器或者机器和服务器之间网络是否连通。
tracert命令显示数据报到达目标主机途中所经过的路径(路由器),并且显示到达每个节点(路由器)的花费时间,显示的信息比ping出来的信息要多,要详细。
编程实现
Code
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.UnknownHostException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
public class CommandUtil {
String tracert = "tracert -h 10 "; //模拟tracert命令
String ping = "ping";//模拟 ping 命令
String routePrint = "route print -4";//模拟route print命令
public static void main(String args[]){
String input = null;
@SuppressWarnings("resource")
Scanner scanner = new Scanner(System.in);
//利用while循环接收输入的命令行参数
while(true){
System.out.println("Please input destination server IP address :\n");
input = scanner.next();
CommandUtil host = new CommandUtil();
host.tracert = host.tracert + " " + input;
host.ping = host.ping + " " + input;
try {
host.command(host.routePrint);
} catch (IOException exception) {
exception.printStackTrace();
}
try {
host.command(host.tracert) ;
} catch (IOException exception) {
exception.printStackTrace();
}
try {
host.command(host.ping);
} catch (IOException exception) {
exception.printStackTrace();
}
InetAddress ipAddress;
try {
ipAddress = InetAddress.getByName(input);
System.out.println("IP address : "+ipAddress);
} catch (UnknownHostException exception) {
exception.printStackTrace();
}
URL url;
try {
url = new URL("http",input,80,"index.html");
System.out.println();//输出服务器地址
System.out.println("Get the Server-Name# : "+url.getHost());
System.out.println();//输出首页文件
System.out.println("Get the default file# : "+url.getFile());
System.out.println();//输出首页协议和端口
System.out.println("Get the protocol# : "+url.getProtocol()+" "+url.getPort());
System.out.println();
} catch (MalformedURLException e) {
e.printStackTrace();
}
System.out.println();
try {
System.out.println("Get serverName & IPAddress:"+InetAddress.getByName(input));
} catch (UnknownHostException e) {
e.printStackTrace();
}
long freeMemory = Runtime.getRuntime().freeMemory();
System.out.println("Surplus memory of JVM: "+freeMemory+"B");
}
}
//模拟 tracert 命令
StringBuffer commandResult = null;
private void command(String tracerCommand) throws IOException{
//第一步:创建进程(是接口不必初始化)
//1.通过Runtime类的getRuntime().exec()传入需要运行的命令参数
System.out.println();
System.out.println(InetAddress.getByName("localhost")+" is tracking the destination server...");
Process process = Runtime.getRuntime().exec(tracerCommand);
readResult(process.getInputStream());
process.destroy();
}
//第二步:通过输入流来将命令执行结果输出到控制台
private void readResult(InputStream inputStream) throws IOException{
commandResult = new StringBuffer(); //初始化命令行
String commandInfo = null; //定义用于接收命令行执行结果的字符串
BufferedReader bufferedReader =
new BufferedReader(new InputStreamReader(inputStream));
while ( (commandInfo = bufferedReader.readLine()) != null) {
System.out.println(commandInfo);
}
bufferedReader.close();
}
}
Result
D:\Java\jdk1.8.0_181\bin\java.exe "-javaagent:D:\JetBrains\IntelliJ IDEA 2020.1.3\lib\idea_rt.jar=39614:D:\JetBrains\IntelliJ IDEA 2020.1.3\bin" -Dfile.encoding=GBK -classpath D:\Java\jdk1.8.0_181\jre\lib\charsets.jar;D:\Java\jdk1.8.0_181\jre\lib\deploy.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\access-bridge-64.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\cldrdata.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\dnsns.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\jaccess.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\jfxrt.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\localedata.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\nashorn.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunec.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunjce_provider.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunmscapi.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\sunpkcs11.jar;D:\Java\jdk1.8.0_181\jre\lib\ext\zipfs.jar;D:\Java\jdk1.8.0_181\jre\lib\javaws.jar;D:\Java\jdk1.8.0_181\jre\lib\jce.jar;D:\Java\jdk1.8.0_181\jre\lib\jfr.jar;D:\Java\jdk1.8.0_181\jre\lib\jfxswt.jar;D:\Java\jdk1.8.0_181\jre\lib\jsse.jar;D:\Java\jdk1.8.0_181\jre\lib\management-agent.jar;D:\Java\jdk1.8.0_181\jre\lib\plugin.jar;D:\Java\jdk1.8.0_181\jre\lib\resources.jar;D:\Java\jdk1.8.0_181\jre\lib\rt.jar;D:\project-java\jiwang\out\production\jiwang CommandUtil
Please input destination server IP address :
baidu.com
localhost/127.0.0.1 is tracking the destination server...
===========================================================================
接口列表
6...b4 b6 86 df cc 9d ......Realtek PCIe GbE Family Controller
11...0c 54 15 fb 33 95 ......Microsoft Wi-Fi Direct Virtual Adapter
14...0e 54 15 fb 33 94 ......Microsoft Wi-Fi Direct Virtual Adapter #2
16...0c 54 15 fb 33 94 ......Intel(R) Dual Band Wireless-AC 3168
17...0c 54 15 fb 33 98 ......Bluetooth Device (Personal Area Network)
1...........................Software Loopback Interface 1
===========================================================================
IPv4 路由表
===========================================================================
活动路由:
网络目标 网络掩码 网关 接口 跃点数
0.0.0.0 0.0.0.0 192.168.2.1 192.168.2.132 35
127.0.0.0 255.0.0.0 在链路上 127.0.0.1 331
127.0.0.1 255.255.255.255 在链路上 127.0.0.1 331
127.255.255.255 255.255.255.255 在链路上 127.0.0.1 331
192.168.2.0 255.255.255.0 在链路上 192.168.2.132 291
192.168.2.132 255.255.255.255 在链路上 192.168.2.132 291
192.168.2.255 255.255.255.255 在链路上 192.168.2.132 291
224.0.0.0 240.0.0.0 在链路上 127.0.0.1 331
224.0.0.0 240.0.0.0 在链路上 192.168.2.132 291
255.255.255.255 255.255.255.255 在链路上 127.0.0.1 331
255.255.255.255 255.255.255.255 在链路上 192.168.2.132 291
===========================================================================
永久路由:
无
localhost/127.0.0.1 is tracking the destination server...
通过最多 10 个跃点跟踪
到 baidu.com [39.156.69.79] 的路由:
1 1 ms 1 ms 1 ms RM2100.lan [192.168.2.1]
2 * * * 请求超时。
3 * * * 请求超时。
4 4 ms 3 ms 3 ms 192.168.100.253
5 4 ms 5 ms 10 ms 192.168.100.114
6 3 ms 3 ms 3 ms 192.168.100.118
7 * * * 请求超时。
8 * * * 请求超时。
9 * * * 请求超时。
10 * * * 请求超时。
跟踪完成。
localhost/127.0.0.1 is tracking the destination server...
正在 Ping baidu.com [39.156.69.79] 具有 32 字节的数据:
来自 39.156.69.79 的回复: 字节=32 时间=20ms TTL=48
来自 39.156.69.79 的回复: 字节=32 时间=20ms TTL=48
来自 39.156.69.79 的回复: 字节=32 时间=20ms TTL=48
来自 39.156.69.79 的回复: 字节=32 时间=20ms TTL=48
39.156.69.79 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 20ms,最长 = 20ms,平均 = 20ms
IP address : baidu.com/39.156.69.79
Get the Server-Name# : baidu.com
Get the default file# : index.html
Get the protocol# : http 80
Get serverName & IPAddress:baidu.com/39.156.69.79
Surplus memory of JVM: 124194640B