您如何知道哪个NIC连接到互联网?

时间:2021-08-20 21:10:55

Consider the following setup: A windows PC with a LAN interface and a WiFi interface (the standard for any new laptop). Each of the interfaces might be connected or disconnected from a network. I need a way to determine which one of the adapters is the one connected to the internet - specifically, in case they are both connected to different networks, one with connection to the internet and one without.

请考虑以下设置:具有LAN接口和WiFi接口的Windows PC(任何新笔记本电脑的标准)。每个接口可能与网络连接或断开。我需要一种方法来确定哪个适配器是连接到互联网的适配器 - 特别是,如果它们都连接到不同的网络,一个连接到互联网,一个没有。

My current solution involves using IPHelper's "GetBestInterface" function and supplying it with the IP address "0.0.0.0".

我目前的解决方案涉及使用IPHelper的“GetBestInterface”功能并为其提供IP地址“0.0.0.0”。

Do you have any other solutions you might suggest to this problem?

您是否有任何其他解决方案可能会对此问题提出建议?

Following some of the answers, let me elaborate:

根据一些答案,让我详细说明:

  • I need this because I have a product that has to choose which adapter to bind to. I have no way of controlling the setup of the network or the host where the product will run and so I need a solution that is as robust as possible, with as few assumptions as possible.
  • 我需要这个,因为我有一个产品必须选择要绑定的适配器。我无法控制网络的设置或产品运行的主机,所以我需要一个尽可能强大的解决方案,尽可能少的假设。

  • I need to do this in code, since this is part of a product.
  • 我需要在代码中执行此操作,因为这是产品的一部分。

@Chris Upchurch: This makes me dependent on google.com being up (usually not a problem) and on any personal firewall that might be installed to allow pinging.

@Chris Upchurch:这让我依赖google.com(通常不是问题)以及任何可能安装允许ping的个人防火墙。

@Till: Like Steve Moon said, relying on the adapter's address is kind of risky because you make a lot of assumptions on the internal network setup.

@Till:就像Steve Moon说的那样,依赖适配器的地址是有点冒险的,因为你对内部网络设置做了很多假设。

@Steve Moon: Looking at the routing table sounds like a good idea, but instead of applying the routing logic myself, I am trying to use "GetBestInterface" as described above. I believe what it should do is exactly what you outlined in your answer, but I am not really sure. The reason I'm reluctant to implement my own "routing logic" is that there's a better chance that I'll get it wrong than if I use a library/API written and tested by more "hard-core" network people.

@Steve Moon:看一下路由表听起来不错,但我不是自己应用路由逻辑,而是尝试使用上面描述的“GetBestInterface”。我相信它应该做的正是你在答案中概述的内容,但我并不确定。我不愿意实现自己的“路由逻辑”的原因是,如果我使用由更多“硬核”网络人员编写和测试的库/ API,那么我错误的机会就会出错。

8 个解决方案

#1


4  

Technically, there is no "connected to the Internet". The real question is, which interface is routeable to a desired address. Right now, you're querying for the "default route" - the one that applies if no specific route to destination exists. But, you're ignoring any specific routes.

从技术上讲,没有“连接到互联网”。真正的问题是,哪个接口可以路由到所需的地址。现在,您正在查询“默认路由” - 如果没有到目的地的特定路由,则应用该路由。但是,你忽略了任何具体的路线。

Fortunately, for 99.9% of home users, that'll do the trick. They're not likely to have much of a routing table, and GetBestInterface will automatically prefer wired over wireless - so you should be good. Throw in an override option for the .1% of cases you screw up, and call it a day.

幸运的是,对于99.9%的家庭用户来说,这就是诀窍。他们不太可能有很多路由表,GetBestInterface会自动更喜欢有线无线 - 所以你应该很好。为你搞砸了的.1%的案例投入覆盖选项,并将其称为一天。

But, for corporate use, you should be using GetBestInterface for a specific destination - otherwise, you'll have issues if someone is on the same LAN as your destination (which means you should take the "internal" interface, not the "external") or has a specific route to your destination (my internal network could peer with your destination's network, for instance).

但是,对于企业用途,您应该将GetBestInterface用于特定目的地 - 否则,如果有人与您的目的地位于同一个LAN上,则会出现问题(这意味着您应该采用“内部”接口,而不是“外部”接口) )或具有到目的地的特定路线(例如,我的内部网络可以与您的目的地网络对等)。

Then again, I'm not sure what you plan to do with this adapter "connected to the Internet", so it might not be a big deal.

然后,我不确定你打算用这个适配器“连接到互联网”做什么,所以这可能不是什么大问题。

#2


2  

I'd look at the routing table. Whichever NIC has an 0.0.0.0 route AND is enabled AND has the lowest metric, is the nic that's currently sending packets to the internet.

我看一下路由表。无论哪个NIC具有0.0.0.0路由并且已启用且具有最低度量标准,则是当前正在向Internet发送数据包的NIC。

So in my case, the top one is the 'internet nic'.

所以在我的情况下,最重要的是'互联网网络'。

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0        10.0.0.10        10.0.0.51     20
          0.0.0.0          0.0.0.0        10.0.0.10        10.0.0.50     25
(much other stuff deleted)

Another alternative is to ping or GetBestInterface 4.2.2.2 - this is an old and venerable DNS server, currently held by GTEI; formerly by Sprint if I remember right.

另一种选择是ping或GetBestInterface 4.2.2.2 - 这是一个古老而古老的DNS服务器,目前由GTEI持有;如果我没记错的话,以前是斯普林特。

#3


1  

Apparently, in Vista there are new interfaces that enable querying for internet connectivity and more. Take a look at the NLM Interfaces and specifically at INetworkConnection - you can specifically query if the network connection has internet connectivity using the GetConnectivity method.

显然,在Vista中有新的接口可以查询互联网连接等等。查看NLM接口,特别是INetworkConnection - 您可以使用GetConnectivity方法专门查询网络连接是否具有Internet连接。

See also: Network Awareness on Windows Vista Unfortunately, this is only available on Vista, so for XP I'd have to keep my original heuristic.

另请参阅:Windows Vista上的网络意识不幸的是,这仅适用于Vista,因此对于XP,我必须保留原始启发式。

#4


0  

Ping google.com though each NIC.

通过每个网卡ping google.com。

#5


0  

Start > Run > cmd.exe (this works in XP and Vista): ipconfig /all

开始>运行> cmd.exe(适用于XP和Vista):ipconfig / all

This displays all info about the interfaces in your computer. The "public" facing interface should have a public IP address. For starters, it should not be 192.168.x.x or 10.x.x.x :)

这将显示有关计算机中接口的所有信息。面向“公共”的接口应具有公共IP地址。首先,它不应该是192.168.x.x或10.x.x.x :)

#6


0  

running traceroute to some public site will show you. Of course, there may be more than one interface that would get you there.

运行traceroute到一些公共站点将显示给你。当然,可能有多个界面可以帮助您。

#7


0  

Look at the routing table? Generally, unless you're routing between the networks in windows (which is possible, but unusual for a client computer these days) the interface that holds the default route is going to have the Internet connection.

看路由表?通常,除非您在Windows中的网络之间路由(这是可能的,但这些天对于客户端计算机来说是不常见的),保持默认路由的接口将具有Internet连接。

Your question didn't detail why or what you're doing this with so I can't provide any specifics. The command line tool "route" may be of some help, but there are probably libraries for whatever programming language you're using to look at the routing table.

你的问题没有详细说明为什么或你正在做什么,所以我不能提供任何细节。命令行工具“route”可能有一些帮助,但是可能存在用于查看路由表的任何编程语言的库。

You can't rely on the IP address of the interface (e.g., assuming an RFC-1918 address [192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8] isn't the internet) since most sites have some kind of NATed firewall or proxy setup and the "internet" interface is really on a "private" lan that gets you out to the Internet.

您不能依赖接口的IP地址(例如,假设RFC-1918地址[192.168.0.0/16,172.16.0.0/12,10.0.0.0/8]不是互联网),因为大多数网站都有某种NAT防火墙或代理设置和“互联网”接口实际上是一个“私人”局域网,可以让你上网。

UPDATE: Based on your further information, it sounds like you have a decent solution. I'm not so sure about the choice of 0.0.0.0 since that's a boundary case for IP address -- might be OK on your particular mix of platform/language. Sounds (from the API description) like you could just specify an address, so why not some address known to be on the Internet, e.g. the IP address of your web site, or something more random like 65.66.67.68? Just make sure not to pick one of the rfc-1918 addresses, or the localhost range (127.0.0.0/8), or multicast, any other reserved range, and any address that resolves to a .mil or .gov (while it doesn't sound like getbestinterface sends any traffic, it would suck to find out by having the feds break your door down... :)

更新:根据您的进一步信息,听起来您有一个不错的解决方案。我不太确定0.0.0.0的选择,因为这是IP地址的边界情况 - 对你特定的平台/语言组合可能没问题。声音(来自API描述)就像您可以指定地址一样,为什么不知道某些地址在互联网上,例如您的网站的IP地址,或更随机的东西,如65.66.67.68?只要确保不选择其中一个rfc-1918地址,或localhost范围(127.0.0.0/8),或多播,任何其他保留范围,以及任何解析为.mil或.gov的地址(当它没有听起来像getbestinterface发送任何流量,它会很难找到联邦调查局打破你的门...... :)

#8


0  

Looking at the network point of view, either could be routing to the "internet" at any time. If things like spanning tree protocol are enabled on a switch then you may find that what may have been the routing card to begin with may not be anymore.

从网络的角度来看,任何一个都可以随时路由到“互联网”。如果在交换机上启用了生成树协议之类的东西,那么您可能会发现可能已经开始的路由卡可能不再存在。

#1


4  

Technically, there is no "connected to the Internet". The real question is, which interface is routeable to a desired address. Right now, you're querying for the "default route" - the one that applies if no specific route to destination exists. But, you're ignoring any specific routes.

从技术上讲,没有“连接到互联网”。真正的问题是,哪个接口可以路由到所需的地址。现在,您正在查询“默认路由” - 如果没有到目的地的特定路由,则应用该路由。但是,你忽略了任何具体的路线。

Fortunately, for 99.9% of home users, that'll do the trick. They're not likely to have much of a routing table, and GetBestInterface will automatically prefer wired over wireless - so you should be good. Throw in an override option for the .1% of cases you screw up, and call it a day.

幸运的是,对于99.9%的家庭用户来说,这就是诀窍。他们不太可能有很多路由表,GetBestInterface会自动更喜欢有线无线 - 所以你应该很好。为你搞砸了的.1%的案例投入覆盖选项,并将其称为一天。

But, for corporate use, you should be using GetBestInterface for a specific destination - otherwise, you'll have issues if someone is on the same LAN as your destination (which means you should take the "internal" interface, not the "external") or has a specific route to your destination (my internal network could peer with your destination's network, for instance).

但是,对于企业用途,您应该将GetBestInterface用于特定目的地 - 否则,如果有人与您的目的地位于同一个LAN上,则会出现问题(这意味着您应该采用“内部”接口,而不是“外部”接口) )或具有到目的地的特定路线(例如,我的内部网络可以与您的目的地网络对等)。

Then again, I'm not sure what you plan to do with this adapter "connected to the Internet", so it might not be a big deal.

然后,我不确定你打算用这个适配器“连接到互联网”做什么,所以这可能不是什么大问题。

#2


2  

I'd look at the routing table. Whichever NIC has an 0.0.0.0 route AND is enabled AND has the lowest metric, is the nic that's currently sending packets to the internet.

我看一下路由表。无论哪个NIC具有0.0.0.0路由并且已启用且具有最低度量标准,则是当前正在向Internet发送数据包的NIC。

So in my case, the top one is the 'internet nic'.

所以在我的情况下,最重要的是'互联网网络'。

IPv4 Route Table
===========================================================================
Active Routes:
Network Destination        Netmask          Gateway       Interface  Metric
          0.0.0.0          0.0.0.0        10.0.0.10        10.0.0.51     20
          0.0.0.0          0.0.0.0        10.0.0.10        10.0.0.50     25
(much other stuff deleted)

Another alternative is to ping or GetBestInterface 4.2.2.2 - this is an old and venerable DNS server, currently held by GTEI; formerly by Sprint if I remember right.

另一种选择是ping或GetBestInterface 4.2.2.2 - 这是一个古老而古老的DNS服务器,目前由GTEI持有;如果我没记错的话,以前是斯普林特。

#3


1  

Apparently, in Vista there are new interfaces that enable querying for internet connectivity and more. Take a look at the NLM Interfaces and specifically at INetworkConnection - you can specifically query if the network connection has internet connectivity using the GetConnectivity method.

显然,在Vista中有新的接口可以查询互联网连接等等。查看NLM接口,特别是INetworkConnection - 您可以使用GetConnectivity方法专门查询网络连接是否具有Internet连接。

See also: Network Awareness on Windows Vista Unfortunately, this is only available on Vista, so for XP I'd have to keep my original heuristic.

另请参阅:Windows Vista上的网络意识不幸的是,这仅适用于Vista,因此对于XP,我必须保留原始启发式。

#4


0  

Ping google.com though each NIC.

通过每个网卡ping google.com。

#5


0  

Start > Run > cmd.exe (this works in XP and Vista): ipconfig /all

开始>运行> cmd.exe(适用于XP和Vista):ipconfig / all

This displays all info about the interfaces in your computer. The "public" facing interface should have a public IP address. For starters, it should not be 192.168.x.x or 10.x.x.x :)

这将显示有关计算机中接口的所有信息。面向“公共”的接口应具有公共IP地址。首先,它不应该是192.168.x.x或10.x.x.x :)

#6


0  

running traceroute to some public site will show you. Of course, there may be more than one interface that would get you there.

运行traceroute到一些公共站点将显示给你。当然,可能有多个界面可以帮助您。

#7


0  

Look at the routing table? Generally, unless you're routing between the networks in windows (which is possible, but unusual for a client computer these days) the interface that holds the default route is going to have the Internet connection.

看路由表?通常,除非您在Windows中的网络之间路由(这是可能的,但这些天对于客户端计算机来说是不常见的),保持默认路由的接口将具有Internet连接。

Your question didn't detail why or what you're doing this with so I can't provide any specifics. The command line tool "route" may be of some help, but there are probably libraries for whatever programming language you're using to look at the routing table.

你的问题没有详细说明为什么或你正在做什么,所以我不能提供任何细节。命令行工具“route”可能有一些帮助,但是可能存在用于查看路由表的任何编程语言的库。

You can't rely on the IP address of the interface (e.g., assuming an RFC-1918 address [192.168.0.0/16, 172.16.0.0/12, 10.0.0.0/8] isn't the internet) since most sites have some kind of NATed firewall or proxy setup and the "internet" interface is really on a "private" lan that gets you out to the Internet.

您不能依赖接口的IP地址(例如,假设RFC-1918地址[192.168.0.0/16,172.16.0.0/12,10.0.0.0/8]不是互联网),因为大多数网站都有某种NAT防火墙或代理设置和“互联网”接口实际上是一个“私人”局域网,可以让你上网。

UPDATE: Based on your further information, it sounds like you have a decent solution. I'm not so sure about the choice of 0.0.0.0 since that's a boundary case for IP address -- might be OK on your particular mix of platform/language. Sounds (from the API description) like you could just specify an address, so why not some address known to be on the Internet, e.g. the IP address of your web site, or something more random like 65.66.67.68? Just make sure not to pick one of the rfc-1918 addresses, or the localhost range (127.0.0.0/8), or multicast, any other reserved range, and any address that resolves to a .mil or .gov (while it doesn't sound like getbestinterface sends any traffic, it would suck to find out by having the feds break your door down... :)

更新:根据您的进一步信息,听起来您有一个不错的解决方案。我不太确定0.0.0.0的选择,因为这是IP地址的边界情况 - 对你特定的平台/语言组合可能没问题。声音(来自API描述)就像您可以指定地址一样,为什么不知道某些地址在互联网上,例如您的网站的IP地址,或更随机的东西,如65.66.67.68?只要确保不选择其中一个rfc-1918地址,或localhost范围(127.0.0.0/8),或多播,任何其他保留范围,以及任何解析为.mil或.gov的地址(当它没有听起来像getbestinterface发送任何流量,它会很难找到联邦调查局打破你的门...... :)

#8


0  

Looking at the network point of view, either could be routing to the "internet" at any time. If things like spanning tree protocol are enabled on a switch then you may find that what may have been the routing card to begin with may not be anymore.

从网络的角度来看,任何一个都可以随时路由到“互联网”。如果在交换机上启用了生成树协议之类的东西,那么您可能会发现可能已经开始的路由卡可能不再存在。