Switching back and forth from static IP to DHCP

Michael

Elite member
Nov 19, 1999
5,435
234
106
I'm in China and my router is set to use VPN for IP addresses less than .100 and not use the VPN for addresses above it (which is the DHCP range). So if I want to use Facebook or another US service like Netflix, I just need to change my IP address into the range that connects to the VPN.

However, it is a royal pain to have to type out all the manual information each time.

Is there an automated way I could do it?

I use Windows 7.

Michael
 
Last edited:

theevilsharpie

Platinum Member
Nov 2, 2009
2,322
14
81
You can automate this using the netsh command-line utility.

Here's a few examples. All of them assume that your interface is named "Local Area Connection". The name of the interface is the name that's displayed in the Adapter Settings section of the control panel. If your interface is named something different, set it accordingly.

To use DHCP:
Code:
netsh interface ipv4 set address "Local Area Connection" dhcp

To use a static address:
Code:
netsh interface ipv4 set address "Local Area Connection" static [IpAddress] [SubnetMask] [DefaultGateway]

For example, if my IP address is 192.168.0.5, my subnet mask is 255.255.255.0, and my default gateway is 192.168.0.1, here's what I'd put in:
Code:
netsh interface ipv4 set address "Local Area Connection" static 192.168.0.5 255.255.255.0 192.168.0.1

You can use these commands quickly by putting them into batch files and then just clicking on them when needed. You can also give them a hotkey if you want to be really fast :awe:
 

Michael

Elite member
Nov 19, 1999
5,435
234
106
The only command I would be missing is setting the DNS (primary and secondary).

Michael
 

seepy83

Platinum Member
Nov 12, 2003
2,132
3
71
Setting DNS through netsh is very similar to the other commands listed.

I don't have a Win 7 machine available right now, but if I remember correctly:
netsh interface ipv4 set dnsservers "Local Area Connection" static #.#.#.# primary

and then to set it back:
netsh interface ipv4 set dnsservers "Local Area Connection" source=dhcp