Page 1 of 1

IP's from netsh command

Posted: 02 Oct 2009 15:12
by vovanius
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!

Posted: 03 Oct 2009 10:50
by vovanius
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