After spending a frustrating evening manually changing the IP settings for
my Ethernet and wireless network adapters numerous times, I decided to create
a tool that would automatically change IP settings. I wanted it to
-
be a command-line tool
-
have easily remembered syntax
-
make default assumptions
-
allow me to change all the IP settings at once
-
allow me to change the IP settings for any card in my system
-
have integrated Help and version information
The result is SetIP.cmd. This script lets you quickly and easily change the
IP address, subnet mask, gateway, and DNS server for Ethernet and wireless network
adapters. When you have more than one adapter of a type (e.g., two Ethernet
network adapters, two wireless network adapters) the script will display the
adapters one by one and let you select the one you want to configure. I've used
SetIP.cmd with various versions of Windows XP. It should work fine with Windows
2000, but I haven't done much testing in that environment. Initial reports indicate
possible problems with using it on server OSs, such as Windows Server 2003.
(I wrote this script for use on client OSs.)
SetIP.cmd uses the Netsh Interface IP commands to change IP settings. Although
these commands are useful, their syntax is complicated and difficult to remember.
So, SetIP.cmd generates the Netsh Interface IP commands for you. You just need
to provide basic information on the command line using syntax that's much easier
to remember.
SetIP.cmd uses the following defaults, unless you specify otherwise on the
command line:
-
It sets the IP settings for an Ethernet network adapter.
-
It sets the IP address to 192.168.1.2.
-
It sets the subnet mask to 255.255.255.0.
-
It sets the gateway to netAddress.1, where netAddress is
the first three octets of the IP address in a 24-bit subnet (i.e., 192.168.1.1
with the default IP address).
-
It sets the DNS server to 4.2.2.2.
You can change the Ethernet network adapter, IP address, subnet mask, gateway,
and DNS defaults by modifying the :PREP section, which Listing
1 shows. (You can download the entire script from the Windows IT Pro
Web site.)
To launch SetIP.cmd, you follow the syntax
Setip [dhcp | <IPAddr>]
[mask <SubnetMask>]
[gw <DefaultGateway>]
[dns <DnsServerAddr>]
/w /nodns
If you launch the script with no parameters, all the default values just specified
are set. When you want the DHCP server to select the IP address, you specify
dhcp. When you want a static IP address, you specify that IP address.
The script has three other optional parameters: mask, gw, and dns. You use
the mask parameter when you want to change the subnet mask to a value other
than the default. The gw parameter lets you set the gateway to a value other
than the default. You use the dns parameter to set the DNS server to a value
other than the default.
The script also has two optional switches: /w and /nodns, which are case-sensitive.
If you're configuring a wireless network adapter rather than an Ethernet network
adapter, you include the /w switch. You use the /nodns switch if you want to
skip configuring the DNS server setting. (During testing, I often just need
to check network connectivity and therefore don't want Netsh to configure the
DNS server setting.)
Let's look at some examples. Suppose you want to set an Ethernet network adapter's
IP address to 192.168.1.2, subnet mask to 255.255.255.0, gateway to 192.168.1.1,
and DNS server to 4.2.2.2. Because the address, subnet mask, gateway, and DNS
server settings are the defaults, you just need to run the command
Setip
If you want to set a wireless network adapter's IP address to 10.20.30.5 and
set its subnet mask, gateway, and DNS server settings to the defaults (255.255.255.0,
10.20.30.1, and 4.2.2.2, respectively), you'd use the command
Setip 10.20.30.5 /w
If you want to set an Ethernet network adapter's IP address to 192.168.2.9,
subnet mask to 255.255.255.128, gateway to 192.168.2.2, and DNS server to 192.168.2.12,
you'd run the command
Setip 192.168.2.9
mask 255.255.255.128
gw 192.168.2.2
dns 192.168.2.12
If you want the DHCP server to automatically set all the IP settings, you'd
use the command
Setip dhcp
Because I frequently use SetIP.cmd to check network connectivity, I often first
set the Ethernet network adapter to a static IP address, then change it back
to an IP address generated by the DHCP server, ignoring the DNS server setting
in both instances. In this scenario, the commands look like
Setip 10.1.1.2 gw 10.1.1.254 /nodns
Setip dhcp /nodns
SetIP.cmd evolved out of my personal testing
in various environments and has served me
well. I hope you find it equally as useful.
—Matthew C. Miller, CTO,
Stability Networks
End of Article
I don't see how this script can work?
jimoler September 19, 2007 (Article Rating: