Windows

如何在 Windows bat 中獲取某些適配器的預設網關?

  • July 12, 2012

請在 windows 中查看下面的 ipconfig 輸出。

C:>ipconfig

Windows IP Configuration


Ethernet adapter Local Area Connection 11:

  Connection-specific DNS Suffix  . :
  Link-local IPv6 Address . . . . . : fe80::4149:4c25:692d:dfec%91
  IPv4 Address. . . . . . . . . . . : 10.252.26.84
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . :

Wireless LAN adapter Wireless Network Connection 15:

  Media State . . . . . . . . . . . : Media disconnected
  Connection-specific DNS Suffix  . :

Ethernet adapter Local Area Connection 10:

  Media State . . . . . . . . . . . : Media disconnected
  Connection-specific DNS Suffix  . :

Wireless LAN adapter Wireless Network Connection 14:

  Connection-specific DNS Suffix  . :
  Link-local IPv6 Address . . . . . : fe80::79a2:afc8:7cd0:79ac%72
  IPv4 Address. . . . . . . . . . . : 192.168.10.9
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . : 192.168.10.1

我想在 bat 文件中找到無線網路連接 14 的預設網關,然後將其儲存在一個變體中以供以後使用

我知道我可以“findstr”,但我不知道如何獲取該 NIC 的預設網關。

謝謝!

使用以下命令驗證介面名稱:

netsh interface ip show address

並嘗試這樣的事情:

@echo off

for /f "tokens=2 delims=:" %%g in ('netsh interface ip show address 
"Wireless Network Connection 14" ^| findstr "Default"') do 
set DefaultGateway=%%g
echo %DefaultGateway%
pause

這應該得到它:

wmic nicconfig where "description like '%wireless%'" get caption, defaultipgateway

引用自:https://serverfault.com/questions/327448