Rem: Scripting a Network-Configuration Change
You can use a simple command-shell script and the Netsh command to script a network-configuration change for a Windows 2000 laptop
July 10, 2002
When I travel, I need to frequently change my Windows 2000 laptop's network configuration from DHCP to a fixed IP address, depending on the configuration of the network that I need to connect to. Can I script this change so that I don't need to manually configure my system's Network Properties?
You can use a simple command-shell script and the Netsh (i.e., NetShell) command. Netsh provides a command-line interface that lets you administer and configure network services, including the ability to change the TCP/IP network configuration settings. The script in Listing 1, page 6, shows how to switch your Win2K laptop to use a dynamic address assigned by a DHCP server.
The script turns off the echo, then the first Netsh command sets the context to interface. The context controls which facet of the network configuration the Netsh command will manipulate. Netsh supports the following contexts: ras, routing, and interface. Using the interface context sets the command to work with the system's networking properties. The ip label specifies that the Netsh command will work with the TCP/IP configuration.
Several subcommands are available within the interface context. In Listing 1, the Set subcommand changes configuration information, and the address label specifies that the Set subcommand will set the system's IP address. The "Local Area Connection" string identifies the connection that these settings will apply to. The name Local Area Connection is the default on most systems, but if your system is different you'll need to change this string to match the connection name that's in use. The dhcp label indicates that a DHCP server will assign the address.
The second Netsh command sets the DNS name of the connection named "Local Area Connection" to be supplied by the DHCP server. When the network uses dynamic addresses assigned through DHCP, DHCP typically assigns the DNS address as well.
When you want to switch your system back to a fixed IP address, you can run the script that Listing 2, page 6. shows. In Listing 2, the first Netsh command sets the IP address of the connection named "Local Area Connection" to the static IP address of 192.168.1.10. The second parameter of the Netsh command specifies the subnet mask. This example uses 255.255.255.0 as the subnet mask associated with the static IP address. The third parameter is the default gateway (if any) on the local IP subnet. Listing 2 gives the gateway a sample address of 192.168.1.1. An optional fourth parameter of the Netsh command is the metric, which defines the maximum number of hops to the default gateway.
The second Netsh command in Listing 2 changes the system's DNS configuration to use a static DNS server at the IP address of 192.168.1.2. You can learn more about the Netsh command from the Microsoft article "Components of NetShell" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netsh/netsh_main_0bn0.asp).
About the Author
You May Also Like