IP's from netsh command

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
vovanius
Posts: 2
Joined: 02 Oct 2009 14:46

IP's from netsh command

#1 Post by vovanius » 02 Oct 2009 15:12

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!

vovanius
Posts: 2
Joined: 02 Oct 2009 14:46

#2 Post by vovanius » 03 Oct 2009 10:50

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

Post Reply