netsh - Scripts to change IP settingsThere was a time when I plugged my Laptop into three different networks, with different IP address spaces, per day. It did not take a long time until I got upset adopting my IP address settings DNS, Subnetmask and Gateway three times a day manually - thanks to clicky windows.Netsh is the solution to this problem. It is a command line tool which is available on a default Windows XP and 2000 installation and therefore no additional third party software will be needed. To "automatically" adopt my IP settings I wrote some small batch files which simply use netsh to change the IP configuration.
setting the IP address, Subnetmask and GatewayThis single line of code sets the IP address, Subnetmask and Gateway of a specific network adapter on any windows machine. It can eigther be executed inside a command line or from a batch script. >
netsh interface ip set address "LAN" static
192.168.1.99 255.255.255.0 192.168.1.1 1
In this specific case the name of the network adapter is 'LAN-Connection', the IP address to be set is 192.168.1.99, the subnetmask is 255.255.255.0 and the default gateway should be set to 192.168.1.1. The name of the network interface can be looked up using the ipconfig command. Don't forget the trailing 1! setting the DNS serverTo set the DNS servers which should be used the following command is used:>
netsh interface ip set dns name="LAN" source=static addr=192.168.100.11 register=primary
> netsh interface ip add dns name="LAN" addr=195.58.161.3 index=2
The first line sets the primary nameserver and the second line sets
the secondary nameserver. If ther is only one nameserver available
the second line can be skipped. using DHCPIn some networks administrators are friendly and offer a DHCP (Dynamic Host Configuration Protocol) Service which sets all IP settings automatically. However to make use of this service the network adapter has to be configured to listen to this protocol. Of course this can also be done with the help of netsh.>
netsh interface ip set address "LAN" dhcp
> netsh interface ip set dns name="LAN" dhcp The first line is used to configure the IP address, subnetmask and gateway automatically with the help of DHCP. However the DNS servers are still not updated. Therfore the second line updates also the DNS settings. putting it all togetherTo have a fully functional network configuration you either have to use DHCP or you have to set all configuration information manually. A sample script for a DHCP environment can be downloaded here and another sample script for a static configuration can be found here.The use of these scripts makes
especially sense when multiple adapters, for instance a LAN and a
wireless interface, have to be configured. However it can also make
life easier when you have to switch between different configurations
very often.
last updated 02 November 2008
|