How to fetch the IP address,subnet,gateway and dns servers from ipconfig /all using batch

I want to fetch IP address, subnet, gateway and DNS servers from ipconfig and store in a variable using bash script. I have created below script but it is not fetching dns servers(below XXXX and YYYY) in separate variables.

 `DNS Servers . . . . . . . . . . . : XXXX YYYY`

echo IPAddress is:
for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "IPv4"') do set ip=%%b
set ip=%ip:~1%
echo %ip%

for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Subnet"') do set sub=%%b
set sub=%sub:~1%
echo Subnet is:
echo %sub%

for /f "tokens=1-2 delims=:" %%a in ('ipconfig^|find "Default"') do set gate=%%b
set gate=%gate:~1%
echo Gateway is:
echo %gate%

0

1 Answer

Refer to this : Get current DNS server used by using Batch code?

@echo off
rem clean variable
set "ip="
rem search data
for /f "tokens=2 delims=:" %%f in ('echo exit^|nslookup 2^>nul') do ( @echo %%f & set "ip=%%f" )
rem remove spaces from variable
set "ip=%ip: =%"
echo DNS_ip = %ip%
pause

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like