Thursday, June 3, 2010

Command-line tips: Configuring IP Addresses

Net SHell (netsh.exe) is a Windows command-line utility (Windows 2000+) that changes network configuration settings on-the-fly. It's scripting-friendly and allows you to do quite a bit on one line of instruction.

Netsh has a kind of "don't ask, don't tell" help system that reminds me a bit of the other command-line administrator tools. For example, for me to find out how to set a static ip address on my Ethernet adapter named "Ethernet", I did the following:

netsh ?

... and it follows with:

"Commands in this context:"
At the bottom of the text returned are 2 important bits of information that look like this:

"The following sub-contexts are available:
bridge diag firewall interface ras routing winsock

To view help for a command, type the command, followed by a space, and then type ?."

What I wanted was to set a configuration on the adapter which for most people would mean "interface". So I typed:

"netsh interface ?"
then ...
"netsh interface ip ?"
then ...
"netsh interface ip set ?"
then ...
"netsh interface ip set address ?"
... and then I see an example ...

" set address name="Local Area Connection" source=dhcp"
" set address local static 10.0.0.9 255.0.0.0 10.0.0.1 1"

I wanted the following configuration on my ethernet adapter:

IP Address ............................................................... : 192.168.1.100
Subnet mask ............................................................ : 255.255.255.0
Default Gateway ..................................................... : 192.168.1.1

My Ethernet adapter was named 'Ethernet', so to get the configuration I wanted, I typed:

"netsh interface ip set address Ethernet static 192.168.1.100 255.255.255.0 192.168.1.1 1"

The last '1' is a metric in the routing table. Netsh took that line and gave back to me an eye-pleasing:
"Ok."

I did whatever I needed to do on the 192.168.1.0/24 network and then I typed in the following line to return the IP configuration to DHCP-assigned:
"netsh interface ip set address name="Ethernet" source=dhcp"

It's a simple tip; easy enough, but it's sharing simple things like this that allow network administrators to get to the fun things (ie: project management, resetting passwords, installing and maintaining software projects, etc.)

We're saving IT administrators brain cells and sleep hours, one post at a time!