Przeglądaj źródła

获取 Linux 中指定网络接口名字的 IP 地址

reghao 11 miesięcy temu
rodzic
commit
1cc64eabda

+ 13 - 11
jdk/src/main/java/cn/reghao/jutil/jdk/machine/id/MachineIdLinux.java

@@ -53,20 +53,22 @@ public class MachineIdLinux implements MachineId {
                 NetworkInterface iface = interfaces.nextElement();
                 NetworkInterface iface = interfaces.nextElement();
                 String ifaceName = iface.getName();
                 String ifaceName = iface.getName();
                 // 过滤掉 localhost 和虚拟网卡
                 // 过滤掉 localhost 和虚拟网卡
-                if (ifaceName.startsWith("lo") || ifaceName.startsWith("docker")
-                        || ifaceName.startsWith("v") || ifaceName.startsWith("br")) {
-                    continue;
-                }
-                Enumeration<InetAddress> inetAddrs = iface.getInetAddresses();
-                while (inetAddrs.hasMoreElements()) {
-                    InetAddress address = inetAddrs.nextElement();
-                    if (!address.isLoopbackAddress()) {
-                        if (address instanceof Inet4Address) {
-                            list.add(address.getHostAddress());
+                if (ifaceName.startsWith("enp")
+                        || ifaceName.startsWith("wlp")
+                        || ifaceName.startsWith("eth")
+                        || ifaceName.startsWith("wlan")
+                        || ifaceName.startsWith("em")) {
+                    Enumeration<InetAddress> inetAddrs = iface.getInetAddresses();
+                    while (inetAddrs.hasMoreElements()) {
+                        InetAddress address = inetAddrs.nextElement();
+                        if (!address.isLoopbackAddress()) {
+                            if (address instanceof Inet4Address) {
+                                list.add(address.getHostAddress());
+                            } else if (address instanceof Inet6Address) {
+                            }
                         }
                         }
                     }
                     }
                 }
                 }
-
             }
             }
         } catch (SocketException e) {
         } catch (SocketException e) {
             e.printStackTrace();
             e.printStackTrace();