Monday, January 21, 2008

Configuring DHCP or Static IP From the Command Line

Configuring DHCP or Static IP From the Command Line:
netsh

netsh is a pretty
useful command-line tool that lets you control tons of things
about your network interfaces and services. One really neat
thing that netsh will do for you is to let you set IP addresses,
DNS and WINS servers. It works on Windows 2000, XP and .NET
Server.

Suppose I have
a laptop that travels between an office in Washington, DC, and
an office in Los Angeles. The DC office uses DHCP. The LA office
uses static IP addresses and when it’s there, the laptop is
supposed to use IP address 192.168.2.10, subnet mask 255.255.255.0,
WINS server 192.168.1.100, DNS server 192.168.1.100.

Whenever I take
the laptop to LA, I’ve got to punch in all kinds of numbers
before the thing will work. When I return to DC, I’ve still
got work to do, as I’ve got to open up TCP/IP properties and
tell the system to stop using static addresses and instead use
DHCP. netsh can help here, as I can use netsh commands in a
batch file; make one batch file for DC and another for LA.

First, I’ll build
the DC batch file. I’ll need three commands. One tells my system
to get its IP address from DHCP, the next says to get its DNS
server from DHCP, and finally the third says to get its WINS
server from DHCP. They look like this:

netsh int ip set
address local source=dhcp

netsh int ip set
dns local source=dhcp

netsh int ip set
wins local source=dhcp

These are the
simpler commands. I just open up Notepad, type them in, and
save the file somewhere on my system’s path as dodc.cmd.

"netsh int
ip set" is the starting point for every one of these commands.
"netsh" is the overall command, and it does lots and
lots of things. But to modify the behavior of a particular network
interface, I use the subcommand "int," which is short
for "interface." Within that, I could do several things,
but in the particular case I want to change the IP settings,
hence the IP, and I want to change ("set") those settings
rather than display them, so I use "set" instead of
"show." By the way, netsh will always give you help
if you ask it. Just type "netsh" all by itself and
your prompt will change from "C:\>" or whatever
to "netsh>;" you can then type "?" to
find out what commands netsh will accept, one of which would
be "int." If you then typed "int" then the
prompt would change to "netsh interface>," and
a "?" would tell you that "IP" was one option,
and so on.

The three commands
pretty much won’t vary from one system to another unless you’ve
got more than one NIC. If you’ve got more than one NIC, then
you’ll want to tell netsh which NIC you’re trying to configure.
In that case, replace the word "local" with the NIC’s
name in quotes, as in

netsh int ip set
address "Local Area Connection 2" source=dhcp

Next, I’ll tackle
the LA batch file. I want to set the IP address to 192.168.2.10
with a subnet mask of 255.255.255.0 and a default gateway of
192.168.2.1. That command looks like this:

netsh int ip set
address local static 192.168.2.10 255.255.255.0 192.168.2.1
2

It starts with
"netsh int ip set address local" as before, but now
instead of "source=dhcp" I specify "static,"
meaning that it’s a static IP address. The three four-quad values
following are, of course, the IP address, subnet mask, and default
gateway. The "2" at the end is the metric for the
default gateway. As it’s at least one hop away from anywhere
in the Internet, I specified "2," but you could set
it to anything that makes sense.

Next, I’ll set
the DNS server to 192.168.1.100. That command looks like this:

netsh int ip set
dns local static 192.168.1.100 primary

Again, the "static"
parameter says that we’re specifying a value rather than using
DHCP. The IP address is of course the IP address of the DNS
server, and "primary" says to do a dynamic DNS registration
on the primary DNS suffix. The alternative to "primary"
is "none," which says not to do any dynamic DNS registrations,
or "both," which means to register on all DNS suffixes.

The command to
set a WINS server is similar:

netsh int ip set
wins local static 192.168.1.100

Just like the
DNS command, except without the primary/none/both option. Collecting
the commands together, we get doLA.cmd:

netsh int ip set
address local static 192.168.2.10 255.255.255.0 192.168.2.1
2

netsh int ip set
dns local static 192.168.1.100 primary

netsh int ip set
wins local static 192.168.1.100

Now when I go
to LA, I just open a command prompt and type DOLA. When I go
to DC, DODC. Very convenient. netsh is a pretty powerful command,
and I hope I’ve inspired you to look at it further!

No comments: