Reputation: 3428
My home network requires that I set my IP address, subnet mask, router IP, and two DNS servers manually. I'd like to write a batch script that would do that for me. I've found some batch files online that allow you to manually enter a static IP, but they don't always have fields for the other information, and I don't want to enter in the IP address every time (those example programs take a user input), I'd rather just double click the file and boom I'm changed to "home" mode.
So what are the batch commands for manually setting an IP address, subnet mask, router IP, and two separate DNS servers to constant values?
Upvotes: 1
Views: 2366
Reputation: 2291
I have written a batch file with below command to set my IP address, subnet mask, gateway address, and two DNS servers. Make sure you run the batch file as an Administrator.
//this set static IP, mask, and gateway address
netsh int ipv4 set address name="Local Area Connection" source=static address=10.127.86.25 mask=255.255.255.240 gateway=10.127.86.30
//this set static DNS for both preffereed and alternate
netsh interface ipv4 add dns "Local Area Connection" address=10.127.70.1 index=1
netsh interface ipv4 add dns "Local Area Connection" address=10.127.70.2 index=2
You can also use below command if only one dns server is required:
netsh int ipv4 set dns name="Local Area Connection" source=static address=10.127.70.1 register=primary validate=no
To set both address and DNS to DHCP so that it automatically provides an IP host, you can use below batch commands:
netsh interface ipv4 set address name="Local Area Connection" source=dhcp
netsh interface ipv4 set dnsservers name="Local Area Connection" source=dhcp
You may need to check all the available networks connections, run the below command for that:
netsh interface show interface
To know more in detail, refer this. Hope that helps you. Let me know if you have any queries.
Upvotes: 1
Reputation: 41234
This is untested and should get you part of the way and you can search for the missing DNS command.
Remove the three >nul
statements while testing so you can see any error responses.
The %%WG111%%
in three places should probably be the text description of your NIC.
From: ten.nigriv
Subject: Re: netsh
Newsgroups: alt.msdos.batch.nt
Date: Sat, 17 Jan 2009 19:54:51 +0000
Before you do I'll quickly explain the three variables, which you'll likely need to change:
1. %G_% is the chosen Default Gateway
On home wireless adapters this is usually the address of the router.
The figure I've used is the default for most Netgear routers.
2. %A_% is your chosen IPAddress
On home systems this is usually set in the range 2-12 of the Gateway Address
3. %S_% is the chosen Subnet Mask
On general home systems this is invariably that which is shown
Here is the script:
::----- START -----
@Echo off & Setlocal enableextensions
Set "G_=192.168.0.1" & Set "A_=192.168.0.7" & Set "S_=255.255.255.0"
>Nul Wmic NICCONFIG WHERE "Description LIKE '%%WG111%%'" CALL EnableStatic "%A_%", "%S_%"
>Nul Wmic NICCONFIG WHERE "Description LIKE '%%WG111%%'" CALL SetGateways "%G_%",1
>Nul Wmic NICCONFIG WHERE "Description LIKE '%%WG111%%'" CALL SetDNSServerSearchOrder "%G_%"
::------ END ------
Upvotes: 0