I recently needed a quick and easy way to change the IP address of my Windows XP workstation. I had to reproduce a problem where the IP address of the client program (which runs on Windows) changes in the middle of a communication session with the server program. Such a thing could happen, for instance, when the client's DHCP lease expires and renewal results in a new address being allocated.

I wanted to do this quickly and easily - the first solution that popped into my head was simply to toggle the configuration of the network interface between using DHCP as normal and a static IP. This can be done via the Network Connections applet in the Control Panel, but is rather involved. It's certainly not quick or easy.

To do the same thing via the command line, use the netsh tool.

After some experimentation, I found that the following two commands were sufficient to give my machine a static IP address and have everything still work ok. The "interface ip set address" command changes the IP to 192.168.1.101 (this address is outside the range allocated by our DHCP server, therefore it will be different to what we had before) and I also had to provide the subnet mask (255.255.255.0), default gateway (192.168.1.5) and a gateway metric. The second command explicitly sets the DNS server to use for name resolution - normally this is done for us by the DHCP server.

netsh interface ip set address "Local Area Connection" static 192.168.1.101 255.255.255.0 192.168.1.5 1
netsh interface ip set dns "Local Area Connection" static 192.168.1.200 primary

To change the interface back to using DHCP and so have it allocated a different IP address

netsh interface ip set address "Local Area Connection" dhcp
netsh interface ip set dns "Local Area Connection" dhcp

Once I'd figured out the commands to use and put them together into two batch files (static.bat and dhcp.bat) I had a method that was sufficiently quick and easy to reproduce the bug I'd started out investigating.

However, this method has a significant drawback. Every time we toggle from static to DHCP or vice versa, the network interface goes down and back up again. This means we lose all connections to network drives, any existing telnet sessions, VNC sessions etc. Fortunately the client and the server I was debugging used a connectionless protocol (not TCP) and so the interface coming down did not prevent them from communicating further. However, with only one desktop machine at my disposal, debugging the server process running on a remote Linux box was proving to be difficult when the ssh session fell down each time.

With a bit of time and a lot of help from free and open source software, this problem can be overcome and we can change the effective IP address of our client program as seen from the server without tearing down all the ancillary connections needed to investigate what's going on. I'll explain how in the next installment.