windows 查找以太网连接的网络适配器名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8626821/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:44:04  来源:igfitidea点击:

Find network adapter name for ethernet connection

javawindowsnetworking

提问by siamii

I need to find the adapter name and default gateway for the ethernet adapter used to connect to the internet.

我需要找到用于连接到 Internet 的以太网适配器的适配器名称和默认网关。

I can get the gateway with netstat -rnbut I'm having problem with the adapter name.

我可以使用网关,netstat -rn但适配器名称有问题。

I'm using java and running a ipconfigcommand for Windows systems and parsing the result. It returns the network adapter names in this format

我正在使用 java 并ipconfig为 Windows 系统运行命令并解析结果。它以这种格式返回网络适配器名称

Ethernet adapter Local Area Connection:

   Connection-specific DNS Suffix  . : customer.service.provider.lan
   Link-local IPv6 Address . . . . . : fb80::7d2b:98aa:506:685%10
   IPv4 Address. . . . . . . . . . . : 10.65.2.21
   Subnet Mask . . . . . . . . . . . : 255.255.192.0
   Default Gateway . . . . . . . . . : 10.65.0.1

I can match the default gateway and like to parse the string "Local Area Connection" for it. On English locale, I can look for the word "adapter" and parse the name until ":". Problem is on computers with different locale than English, "adapter" is called something else. How can I reliably obtain the network adapter name?

我可以匹配默认网关,并喜欢为它解析字符串“本地连接”。在英语语言环境中,我可以查找单词“adapter”并解析名称直到“:”。问题出在语言环境与英语不同的计算机上,“适配器”被称为其他东西。如何可靠地获取网络适配器名称?

回答by Thor

Take a look at this tutorial: Listing Network Interface Addresses And use getDisplayName() instand of getName(). See Heer to get the Gateway: How can I determine the IP of my router/gateway in Java?

看看本教程:列出网络接口地址并使用 getDisplayName() 代替 getName()。请参阅 Heer 以获取网关:如何在 Java 中确定路由器/网关的 IP?