how to get default gateway of certain adapter in windows bat?
Please see below ipconfig output in windows.
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
I want to find the Default Gateway for Wireless Network Connection 14 in a bat file then store that in a varient to use later
I understand I can "findstr" but I have no idea how to get the default gateway of that NIC.
Thanks!
Verify the interface name with:
netsh interface ip show address
and try something like this:
@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
This should get it:
wmic nicconfig where "description like '%wireless%'" get caption, defaultipgateway
Try something along the lines of:
@For /f "tokens=3" %%* in (
'route.exe print ^|findstr "\<0.0.0.0\>"'
) Do @Set "DefaultGateway=%%*"
You should then be able to use %DefaultGateway% as a variable.
Source