Hello All! I have a problem. I need to use primary and secondary DNS servers IPs in cmd script. But how can I get them and assign to corresponding variables? I tried so:
Command
netsh interface ip show dns "Internet"
gives the output:
Configuration for interface "Internet"
Statically Configured DNS Servers: xxx.xxx.xxx.xxx
yyy.yyy.yyy.yyy
Register with which suffix: None
I tried to cut IPs like this:
for /f "skip=2 tokens=5" %%i in ('netsh interface ip show dns "Internet"') do set primary=%%i
result is:
set primary=xxx.xxx.xxx.xxx -correct, but then is overwritten
set primary=None
for /f "skip=3 tokens=1" %%i in ('netsh interface ip show dns "Internet"') do set secondary=%%i
result is:
set secondary=yyy.yyy.yyy.yyy -correct, but then is overwritten
set secondary=Register
How can I tell script not to process the last line
"Register with which suffix: None" ???
Thanks!
IP's from netsh command
Moderator: DosItHelp
Solved:
@echo off
call :get_dns_servers
echo %primary_dns_server%
echo %secondary_dns_server%
pause
exit
:get_dns_servers
for /f "skip=2 tokens=5" %%i in ('netsh interface ip show dns %connection_name%') do set primary_dns_server=%%i & goto get_secondary
:get_secondary
for /f "skip=3 tokens=1" %%i in ('netsh interface ip show dns %connection_name%') do set secondary_dns_server=%%i & goto :eof
@echo off
call :get_dns_servers
echo %primary_dns_server%
echo %secondary_dns_server%
pause
exit
:get_dns_servers
for /f "skip=2 tokens=5" %%i in ('netsh interface ip show dns %connection_name%') do set primary_dns_server=%%i & goto get_secondary
:get_secondary
for /f "skip=3 tokens=1" %%i in ('netsh interface ip show dns %connection_name%') do set secondary_dns_server=%%i & goto :eof