使用Java 1.5在Windows中获取以太网适配器的IPv4地址

前端之家收集整理的这篇文章主要介绍了使用Java 1.5在Windows中获取以太网适配器的IPv4地址前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题

我的Windows系统有多个以太网适配器.鉴于以太网适配器的名称,我需要找到它的IP地址.

例如,我系统的ipconfig命令的输出是:

  1. Ethernet adapter GB1:
  2.  
  3. Connection-specific DNS Suffix . :
  4. IP Address. . . . . . . . . . . . : 0.0.0.0
  5. Subnet Mask . . . . . . . . . . . : 0.0.0.0
  6. Default Gateway . . . . . . . . . :
  7.  
  8. Ethernet adapter SWITCH:
  9.  
  10. Connection-specific DNS Suffix . :
  11. IP Address. . . . . . . . . . . . : 10.200.1.11
  12. Subnet Mask . . . . . . . . . . . : 255.255.255.0
  13. IP Address. . . . . . . . . . . . : 10.200.1.51
  14. Subnet Mask . . . . . . . . . . . : 255.255.255.0
  15. Default Gateway . . . . . . . . . :
  16.  
  17. Ethernet adapter LAN:
  18.  
  19. Connection-specific DNS Suffix . :
  20. IP Address. . . . . . . . . . . . : 10.1.2.62
  21. Subnet Mask . . . . . . . . . . . : 255.255.254.0
  22. IP Address. . . . . . . . . . . . : 10.1.2.151
  23. Subnet Mask . . . . . . . . . . . : 255.255.254.0
  24. Default Gateway . . . . . . . . . : 10.1.2.1

注意:我不必担心无线适配器或任何其他类型的适配器.我只需要为以太网适配器执行此操作.

对于这个系统,我需要编写一个行为如下所示的Java类:

  1. C:>java NameToIp GB1
  2. 0.0.0.0
  3.  
  4. C:>java NameToIp SWITCH
  5. 10.200.1.11
  6. 10.200.1.51
  7.  
  8. C:>java NameToIp LAN
  9. 10.1.2.62
  10. 10.1.2.151

什么行不通

使用java.net.NetworkInterface没有帮助.它的getName()getDisplayName()方法不会打印出现在ipconfig输出或Windows网络连接中的适配器连接名称.它们会打印实际的设备名称.例如,请考虑以下代码

  1. import java.util.Enumeration;
  2. import java.net.InetAddress;
  3. import java.net.NetworkInterface;
  4. import java.net.SocketException;
  5. import java.net.UnknownHostException;
  6.  
  7. public class ListInterfaces
  8. {
  9. public static void main(String[] args) throws SocketException,UnknownHostException {
  10.  
  11. Enumeration<NetworkInterface> nwInterfaces = NetworkInterface.getNetworkInterfaces();
  12.  
  13. while (nwInterfaces.hasMoreElements()) {
  14.  
  15. NetworkInterface nwInterface = nwInterfaces.nextElement();
  16. System.out.print(nwInterface.getName() + ": " +
  17. nwInterface.getDisplayName());
  18.  
  19. Enumeration<InetAddress> addresses = nwInterface.getInetAddresses();
  20. while (addresses.hasMoreElements()) {
  21. InetAddress address = addresses.nextElement();
  22. System.out.print(" - " + address.getHostAddress());
  23. }
  24. System.out.println();
  25. }
  26. }
  27. }

这将打印以下输出

  1. C:>java ListInterfaces
  2. lo: MS TCP Loopback interface - 127.0.0.1
  3. eth0: Broadcom BCM5709C NetXtreme II GigE (NDIS VBD Client) #
  4. eth1: Broadcom BCM5709C NetXtreme II GigE (NDIS VBD Client) #2 - 10.200.1.11 - 10.200.1.51
  5. eth2: Broadcom BCM5709C NetXtreme II GigE (NDIS VBD Client) #3 - 10.1.2.62 - 10.1.2.151

一个丑陋的黑客行事

我编写了一个丑陋的黑客,它从ipconfig的输出提取指定适配器名称的IP地址.这是代码.

  1. import java.util.ArrayList;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.InputStream;
  5. import java.io.IOException;
  6.  
  7. public class NameToIp
  8. {
  9. public static ArrayList<String> getIP(String adapterName)
  10. throws IOException,InterruptedException
  11. {
  12. // Run the Windows 'ipconfig' command and get its stdout
  13. ProcessBuilder cmdBuilder = new ProcessBuilder("ipconfig");
  14. Process process = cmdBuilder.start();
  15. BufferedReader stdout = new BufferedReader(
  16. new InputStreamReader(process.getInputStream()));
  17.  
  18. // Find the section for the specified adapter
  19. String line;
  20. boolean foundAdapter = false;
  21. while ((line = stdout.readLine()) != null) {
  22. line = line.trim();
  23. if (line.equals("Ethernet adapter " + adapterName + ':')) {
  24. foundAdapter = true;
  25. break;
  26. }
  27. }
  28. if (!foundAdapter) {
  29. process.waitFor();
  30. throw new IOException("Adapter not found");
  31. }
  32.  
  33. // Find IP addresses in the found section
  34. ArrayList<String> ips = new ArrayList<String>();
  35. while ((line = stdout.readLine()) != null) {
  36. // Stop parsing if we reach the beginning of the next
  37. // adapter section in the output of ifconfig
  38. if (line.length() > 0 && line.charAt(0) != ' ') {
  39. break;
  40. }
  41.  
  42. line = line.trim();
  43.  
  44. // Extract IP addresses
  45. if (line.startsWith("IP Address.") ||
  46. line.startsWith("IPv4 Address.")) {
  47.  
  48. int colonIndex;
  49. if ((colonIndex = line.indexOf(':')) != 1) {
  50. ips.add(line.substring(colonIndex + 2));
  51. }
  52. }
  53. }
  54. process.waitFor();
  55.  
  56. return ips;
  57. }
  58.  
  59. public static void main(String[] args)
  60. throws IOException,InterruptedException
  61. {
  62. // Print help message if adapter name has not been specified
  63. if (args.length != 1) {
  64. StackTraceElement[] stack = Thread.currentThread().getStackTrace();
  65. String prog = stack[stack.length - 1].getClassName();
  66.  
  67. System.err.println("Usage: java " + prog + " ADAPTERNAME");
  68. System.err.println("Examples:");
  69. System.err.println(" java " + prog +" \"Local Area Connection\"");
  70. System.err.println(" java " + prog +" LAN");
  71. System.err.println(" java " + prog +" SWITCH");
  72. System.exit(1);
  73. }
  74.  
  75. ArrayList<String> ips = getIP(args[0]);
  76. for (String ip: ips) {
  77. System.out.println(ip);
  78. }
  79. }
  80. }

有没有更好的方法解决这个问题?

解决方法

创建一个使用Windows API查询本地以太网地址并使用JNI调用dll的DLL.

猜你在找的Java相关文章