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?
netsh dns and IP settings!
Moderator: DosItHelp
Re: netsh dns and IP settings!
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.
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?
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?