netsh dns and IP settings!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
merrimanmerlin
Posts: 1
Joined: 17 Dec 2010 07:09

netsh dns and IP settings!

#1 Post by merrimanmerlin » 17 Dec 2010 07:16

Hey, I'm trying to create two batch files that will automatically modify my network settings when I travel from work to home. I have researched and tried to do it myself, but I can't get it to work flawlessly. The settings I am trying to achieve are as follows:

Home: Static IP and DNS
IP: 192.168.2.15
subnet: 255.255.255.0
gateway: 192.168.2.1
DNS: 192.168.2.1

This is the code I have made myself that does not work:
@echo off
netsh interface ip set address "wireless network connection" static 192.168.2.15 255.255.255.0 192.168.2.1
netsh interface ip set dns “wireless network connection” static 192.168.2.1

Work (or anywhere else):dynamic
ip: auto
dns: auto

this is the code I have used for this that does not work:
@echo off
netsh interface ip set address "wireless network connection" dhcp
netsh interface ip set dns “wireless network Connection” dhcp
exit

And I always run them as administrator, so that is not the problem. Does anyone know what I am doing wrong?

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

Re: netsh dns and IP settings!

#2 Post by DosItHelp » 20 Dec 2010 23:05

merrimanmerlin,

I tried same in the past and could not get this working without using the "-f ScriptFile" option. I didn't like the idea ending up with two files, a netsh script file and a batch file that runs the script, so I played around and found a way to embed the netsh ScriptFile into the batch file.
With that I was then able to write a couple batch files, each setting a different IP configuration.
You would need copy this script into a batch file, e.g. named "ConnectHome.cmd" to adapt this example to your needs.

Code: Select all

@netsh -f %~f0&PAUSE&GOTO:EOF
# ----------------------------------
# Interface IP Configuration         
# ----------------------------------
pushd interface ip

# Interface IP Configuration for "Wireless Network Connection 3"

set address name="Wireless Network Connection 3" source=static addr=192.168.2.15 mask=255.255.255.0
set address name="Wireless Network Connection 3" gateway=192.168.2.1 gwmetric=0
set dns name="Wireless Network Connection 3" source=static addr=192.168.2.1 register=PRIMARY
set wins name="Wireless Network Connection 3" source=static addr=none

popd
# End of interface IP configuration

A better starting point for creating a netsch script is to run "netsh dump" and copy/pipe the result into a batch file. This will output a netsh script to restore all your adapter configurations, so simply remove all adapter configuration that you don't need and leave only the once you want to overwrite. Then add "@netsh -f "%~f0"&PAUSE&GOTO:EOF" as the starting line of same batch file.
Good luck - let me know if it worked for you!
DosItHelp? :wink:

Post Reply