如何在 Windows Bat 或 Powershell 中识别用于成功 ping 的网络适配器(或接口)名称

问题描述 投票:0回答:2

有没有办法识别用于成功 ping 的网络适配器(或接口)名称

例如我有以下适配器:

我执行命令

ping google.com

成功了,我想知道使用的适配器是“Wi-Fi”Sunrise_5GHz_387918

c# 也有类似的问题 识别活动网络接口

但我正在寻找一种Windows批处理文件方式(可能是powershell)。

powershell batch-file networking
2个回答
2
投票

您可以在 PS 中使用:

get-wmiobject win32_networkadapter | select netconnectionid, name, InterfaceIndex, netconnectionstatus

或者, 您可以使用

netsh

netsh interface ipv4 show interfaces

在Windows 8等较新版本的PS中,您甚至可以直接使用:

Get-NetAdapter | SELECT name, status, speed, fullduplex | where status -eq 'up'

请注意,netconnectionstatus值指示连接状态。 2表示

connected


0
投票

在 PowerShell 中,您可以使用

Test-NetConnection
cmdlet 确定使用哪个网络适配器对特定地址执行 ping 操作。返回的对象将具有多个属性来标识所使用的适配器,包括:

  • InterfaceAlias
    :适配器的名称
  • InterfaceIndex
    :适配器索引
  • NetAdapter
    :代表网络适配器的对象,类似于
    Get-NetAdapter

要获取成功 ping Google 的适配器,您可以使用以下命令:

$NetAdapter = Test-NetConnection -ComputerName 8.8.8.8
$NetAdapter.InterfaceAlias  # Returns (e.g.) 'Ethernet 2'
$NetAdapter.InterfaceIndex  # Returns (e.g.) 15
© www.soinside.com 2019 - 2024. All rights reserved.