ping和tracert命令

ping命令一般用来测试两台机器或者机器和服务器之间网络是否连通。
image.png
tracert命令显示数据报到达目标主机途中所经过的路径(路由器),并且显示到达每个节点(路由器)的花费时间,显示的信息比ping出来的信息要多,要详细。
image.png

编程实现

Code

  1. import java.net.InetAddress;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. import java.net.UnknownHostException;
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.InputStreamReader;
  9. import java.util.Scanner;
  10. public class CommandUtil {
  11. String tracert = "tracert -h 10 "; //模拟tracert命令
  12. String ping = "ping";//模拟 ping 命令
  13. String routePrint = "route print -4";//模拟route print命令
  14. public static void main(String args[]){
  15. String input = null;
  16. @SuppressWarnings("resource")
  17. Scanner scanner = new Scanner(System.in);
  18. //利用while循环接收输入的命令行参数
  19. while(true){
  20. System.out.println("Please input destination server IP address :\n");
  21. input = scanner.next();
  22. CommandUtil host = new CommandUtil();
  23. host.tracert = host.tracert + " " + input;
  24. host.ping = host.ping + " " + input;
  25. try {
  26. host.command(host.routePrint);
  27. } catch (IOException exception) {
  28. exception.printStackTrace();
  29. }
  30. try {
  31. host.command(host.tracert) ;
  32. } catch (IOException exception) {
  33. exception.printStackTrace();
  34. }
  35. try {
  36. host.command(host.ping);
  37. } catch (IOException exception) {
  38. exception.printStackTrace();
  39. }
  40. InetAddress ipAddress;
  41. try {
  42. ipAddress = InetAddress.getByName(input);
  43. System.out.println("IP address : "+ipAddress);
  44. } catch (UnknownHostException exception) {
  45. exception.printStackTrace();
  46. }
  47. URL url;
  48. try {
  49. url = new URL("http",input,80,"index.html");
  50. System.out.println();//输出服务器地址
  51. System.out.println("Get the Server-Name# : "+url.getHost());
  52. System.out.println();//输出首页文件
  53. System.out.println("Get the default file# : "+url.getFile());
  54. System.out.println();//输出首页协议和端口
  55. System.out.println("Get the protocol# : "+url.getProtocol()+" "+url.getPort());
  56. System.out.println();
  57. } catch (MalformedURLException e) {
  58. e.printStackTrace();
  59. }
  60. System.out.println();
  61. try {
  62. System.out.println("Get serverName & IPAddress:"+InetAddress.getByName(input));
  63. } catch (UnknownHostException e) {
  64. e.printStackTrace();
  65. }
  66. long freeMemory = Runtime.getRuntime().freeMemory();
  67. System.out.println("Surplus memory of JVM: "+freeMemory+"B");
  68. }
  69. }
  70. //模拟 tracert 命令
  71. StringBuffer commandResult = null;
  72. private void command(String tracerCommand) throws IOException{
  73. //第一步:创建进程(是接口不必初始化)
  74. //1.通过Runtime类的getRuntime().exec()传入需要运行的命令参数
  75. System.out.println();
  76. System.out.println(InetAddress.getByName("localhost")+" is tracking the destination server...");
  77. Process process = Runtime.getRuntime().exec(tracerCommand);
  78. readResult(process.getInputStream());
  79. process.destroy();
  80. }
  81. //第二步:通过输入流来将命令执行结果输出到控制台
  82. private void readResult(InputStream inputStream) throws IOException{
  83. commandResult = new StringBuffer(); //初始化命令行
  84. String commandInfo = null; //定义用于接收命令行执行结果的字符串
  85. BufferedReader bufferedReader =
  86. new BufferedReader(new InputStreamReader(inputStream));
  87. while ( (commandInfo = bufferedReader.readLine()) != null) {
  88. System.out.println(commandInfo);
  89. }
  90. bufferedReader.close();
  91. }
  92. }

Result

  1. 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
  2. Please input destination server IP address
  3. baidu.com
  4. localhost/127.0.0.1 is tracking the destination server...
  5. ===========================================================================
  6. 接口列表
  7. 6...b4 b6 86 df cc 9d ......Realtek PCIe GbE Family Controller
  8. 11...0c 54 15 fb 33 95 ......Microsoft Wi-Fi Direct Virtual Adapter
  9. 14...0e 54 15 fb 33 94 ......Microsoft Wi-Fi Direct Virtual Adapter #2
  10. 16...0c 54 15 fb 33 94 ......Intel(R) Dual Band Wireless-AC 3168
  11. 17...0c 54 15 fb 33 98 ......Bluetooth Device (Personal Area Network)
  12. 1...........................Software Loopback Interface 1
  13. ===========================================================================
  14. IPv4 路由表
  15. ===========================================================================
  16. 活动路由:
  17. 网络目标 网络掩码 网关 接口 跃点数
  18. 0.0.0.0 0.0.0.0 192.168.2.1 192.168.2.132 35
  19. 127.0.0.0 255.0.0.0 在链路上 127.0.0.1 331
  20. 127.0.0.1 255.255.255.255 在链路上 127.0.0.1 331
  21. 127.255.255.255 255.255.255.255 在链路上 127.0.0.1 331
  22. 192.168.2.0 255.255.255.0 在链路上 192.168.2.132 291
  23. 192.168.2.132 255.255.255.255 在链路上 192.168.2.132 291
  24. 192.168.2.255 255.255.255.255 在链路上 192.168.2.132 291
  25. 224.0.0.0 240.0.0.0 在链路上 127.0.0.1 331
  26. 224.0.0.0 240.0.0.0 在链路上 192.168.2.132 291
  27. 255.255.255.255 255.255.255.255 在链路上 127.0.0.1 331
  28. 255.255.255.255 255.255.255.255 在链路上 192.168.2.132 291
  29. ===========================================================================
  30. 永久路由:
  31. localhost/127.0.0.1 is tracking the destination server...
  32. 通过最多 10 个跃点跟踪
  33. baidu.com [39.156.69.79] 的路由:
  34. 1 1 ms 1 ms 1 ms RM2100.lan [192.168.2.1]
  35. 2 * * * 请求超时。
  36. 3 * * * 请求超时。
  37. 4 4 ms 3 ms 3 ms 192.168.100.253
  38. 5 4 ms 5 ms 10 ms 192.168.100.114
  39. 6 3 ms 3 ms 3 ms 192.168.100.118
  40. 7 * * * 请求超时。
  41. 8 * * * 请求超时。
  42. 9 * * * 请求超时。
  43. 10 * * * 请求超时。
  44. 跟踪完成。
  45. localhost/127.0.0.1 is tracking the destination server...
  46. 正在 Ping baidu.com [39.156.69.79] 具有 32 字节的数据:
  47. 来自 39.156.69.79 的回复: 字节=32 时间=20ms TTL=48
  48. 来自 39.156.69.79 的回复: 字节=32 时间=20ms TTL=48
  49. 来自 39.156.69.79 的回复: 字节=32 时间=20ms TTL=48
  50. 来自 39.156.69.79 的回复: 字节=32 时间=20ms TTL=48
  51. 39.156.69.79 Ping 统计信息:
  52. 数据包: 已发送 = 4,已接收 = 4,丢失 = 0 (0% 丢失),
  53. 往返行程的估计时间(以毫秒为单位):
  54. 最短 = 20ms,最长 = 20ms,平均 = 20ms
  55. IP address : baidu.com/39.156.69.79
  56. Get the Server-Name# : baidu.com
  57. Get the default file# : index.html
  58. Get the protocol# : http 80
  59. Get serverName & IPAddressbaidu.com/39.156.69.79
  60. Surplus memory of JVM: 124194640B