Link to home
Start Free TrialLog in
Avatar of Priscilla_Hora
Priscilla_Hora

asked on

Netsh - script to check DHCP enabled

How can I create a script that does something like this...

If computer has dynamic ip then  run netsh interface ip set dns "Local Area Connection" dhcp

else if computer has a static ip, assign dns of 10.0.0.3 as prefered dns..

Does this make sense? How would I put this into a batch or vbs file?

Please refer to question - https://www.experts-exchange.com/questions/21326923/Change-DNS-to-Obtain-dns-server-address-automatically.html

CODE should be something like?

IF IP = DHCP:Enabled
netsh interface ip set dns "Local Area Connection" dhcp
ELSE
RUN netsh interface ip set dns "Local Area Connection" static 10.0.0.3

Thanks
Avatar of stevenlewis
stevenlewis

I sent off an email to lee, telling him about this question :-)
Avatar of Lee W, MVP
I'm well aware of NETSH, but I'm not great with using it.  I'll therefore assume you have the NETSH commands accurate.

The if part you are speaking of should be relatively easy.  There are a few approaches to this, here's one using an environment variable:

for /f "tokens=3 delims=:. " %%a in ('ipconfig /all ^| find /i "dhcp enabled"') do set DHCP=%a
If /i "%dhcp%" == "Yes" (
    netsh interface ip set dns "Local Area Connection" dhcp
) Else (
    netsh interface ip set dns "Local Area Connection" static 10.0.0.3
)
You could actually avoid the environment variable entirely with this:

for /f "tokens=3 delims=:. " %%a in ('ipconfig /all ^| find /i "dhcp enabled"') do (
If /i "%%a" == "Yes" (
    netsh interface ip set dns "Local Area Connection" dhcp
) Else (
    netsh interface ip set dns "Local Area Connection" static 10.0.0.3
)
)


Ooops - a small typo in the first comment:

Change the ... set DHCP=%a to set DHCP=%%a
Lee, to give you some more to work with
netsh interface ip show config
results in an output of something like



C:\Documents and Settings\Me>netsh interface ip show config

Configuration for interface "Local Area Connection"
    DHCP enabled:                         No
    IP Address:                           192.168.1.12
    SubnetMask:                           255.255.255.0
    Default Gateway:                      192.168.1.250
    GatewayMetric:                        0
    InterfaceMetric:                      0
    Statically Configured DNS Servers:    66.73.20.40
                                          206.141.193.55
    Statically Configured WINS Servers:   None
    Register with which suffix:           Primary only
ASKER CERTIFIED SOLUTION
Avatar of Lee W, MVP
Lee W, MVP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
> else if computer has a static ip, assign dns of 10.0.0.3 as prefered dns..

Why not just change the DNS address in your DHCP scope ?
That way, all DHCP members will get a different DNS address, but anyone who isn't configured for DHCP will get the old one ?
Avatar of Priscilla_Hora

ASKER

You're a genius Leew!! Thanks for you help! You saved me hours trying to sort this out!
Told you he was good LOL
Good job Lee!
Thanks, glad I could help.  And thanks to stevenlewis for referring me to the question.
I have some simplier but similar problem:

i created batch script:

@Echo on
netsh interface ip set dns name="Wireless Network Connection" source=dhcp register=none
pause

and if i double click it, it keeps on excecuting this line forever:
netsh interface ip set dns name="Wireless Network Connection" source=dhcp register=none

But if i type it in manually then goes fine.

Any suggestions?