tag:blogger.com,1999:blog-7495121580708212210.post-65120493181771729422008-01-21T20:34:00.000-07:002008-01-21T20:35:04.167-07:002008-01-21T20:35:04.167-07:00Configuring DHCP or Static IP From the Command Line<p><span style="font-family:Tahoma;font-size:85%;"><strong>Configuring DHCP or Static IP From the Command Line:<br />netsh</strong></span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh is a pretty<br />useful command-line tool that lets you control tons of things<br />about your network interfaces and services. One really neat<br />thing that netsh will do for you is to let you set IP addresses,<br />DNS and WINS servers. It works on Windows 2000, XP and .NET<br />Server.</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">Suppose I have<br />a laptop that travels between an office in Washington, DC, and<br />an office in Los Angeles. The DC office uses DHCP. The LA office<br />uses static IP addresses and when it’s there, the laptop is<br />supposed to use IP address 192.168.2.10, subnet mask 255.255.255.0,<br />WINS server 192.168.1.100, DNS server 192.168.1.100.</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">Whenever I take<br />the laptop to LA, I’ve got to punch in all kinds of numbers<br />before the thing will work. When I return to DC, I’ve still<br />got work to do, as I’ve got to open up TCP/IP properties and<br />tell the system to stop using static addresses and instead use<br />DHCP. netsh can help here, as I can use netsh commands in a<br />batch file; make one batch file for DC and another for LA.</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">First, I’ll build<br />the DC batch file. I’ll need three commands. One tells my system<br />to get its IP address from DHCP, the next says to get its DNS<br />server from DHCP, and finally the third says to get its WINS<br />server from DHCP. They look like this:</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />address local source=dhcp</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />dns local source=dhcp</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />wins local source=dhcp</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">These are the<br />simpler commands. I just open up Notepad, type them in, and<br />save the file somewhere on my system’s path as dodc.cmd.</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">"netsh int<br />ip set" is the starting point for every one of these commands.<br />"netsh" is the overall command, and it does lots and<br />lots of things. But to modify the behavior of a particular network<br />interface, I use the subcommand "int," which is short<br />for "interface." Within that, I could do several things,<br />but in the particular case I want to change the IP settings,<br />hence the IP, and I want to change ("set") those settings<br />rather than display them, so I use "set" instead of<br />"show." By the way, netsh will always give you help<br />if you ask it. Just type "netsh" all by itself and<br />your prompt will change from "C:\>" or whatever<br />to "netsh>;" you can then type "?" to<br />find out what commands netsh will accept, one of which would<br />be "int." If you then typed "int" then the<br />prompt would change to "netsh interface>," and<br />a "?" would tell you that "IP" was one option,<br />and so on.</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">The three commands<br />pretty much won’t vary from one system to another unless you’ve<br />got more than one NIC. If you’ve got more than one NIC, then<br />you’ll want to tell netsh which NIC you’re trying to configure.<br />In that case, replace the word "local" with the NIC’s<br />name in quotes, as in</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />address "Local Area Connection 2" source=dhcp</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">Next, I’ll tackle<br />the LA batch file. I want to set the IP address to 192.168.2.10<br />with a subnet mask of 255.255.255.0 and a default gateway of<br />192.168.2.1. That command looks like this:</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />address local static 192.168.2.10 255.255.255.0 192.168.2.1<br />2</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">It starts with<br />"netsh int ip set address local" as before, but now<br />instead of "source=dhcp" I specify "static,"<br />meaning that it’s a static IP address. The three four-quad values<br />following are, of course, the IP address, subnet mask, and default<br />gateway. The "2" at the end is the metric for the<br />default gateway. As it’s at least one hop away from anywhere<br />in the Internet, I specified "2," but you could set<br />it to anything that makes sense.</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">Next, I’ll set<br />the DNS server to 192.168.1.100. That command looks like this:</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />dns local static 192.168.1.100 primary</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">Again, the "static"<br />parameter says that we’re specifying a value rather than using<br />DHCP. The IP address is of course the IP address of the DNS<br />server, and "primary" says to do a dynamic DNS registration<br />on the primary DNS suffix. The alternative to "primary"<br />is "none," which says not to do any dynamic DNS registrations,<br />or "both," which means to register on all DNS suffixes.</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">The command to<br />set a WINS server is similar:</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />wins local static 192.168.1.100</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">Just like the<br />DNS command, except without the primary/none/both option. Collecting<br />the commands together, we get doLA.cmd:</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />address local static 192.168.2.10 255.255.255.0 192.168.2.1<br />2</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />dns local static 192.168.1.100 primary</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">netsh int ip set<br />wins local static 192.168.1.100</span> </p> <p align="justify"><span style="font-family:Tahoma;font-size:85%;">Now when I go<br />to LA, I just open a command prompt and type DOLA. When I go<br />to DC, DODC. Very convenient. netsh is a pretty powerful command,<br />and I hope I’ve inspired you to look at it further!</span> </p>Jason Caronenoreply@blogger.com