Search This Blog

Thursday, October 23, 2008

Netsh: Awesome but hardly known/used Windows command line tool, netsh.

Netsh is a wonderful command that I have known about for a while but have just finally realized how to properly format the command so that it actually does what I want it to do.

Netsh is simply a command-line (runs inside Command Prompt) tool that allows you to manipulate or change the settings for your wired and wireless network cards.

What I really find awesome with this tool is how it can allow you to use a batch file to set a static IP along with the netmask, gateway, and DNS servers. You can even setup a batch file to turn windows firewall off, a nice feature to incorporate in to a logon script if it is necessary to turn the firewall off for remote administration or something else.

An example of how to set a static IP with a batch file is as follows:
First Line: netsh interface ip set address "Local Area Connection" static 192.168.1.100 255.255.255.0 192.168.1.1 0

Second Line: netsh interface ip set dns "Local Area Connection" static 192.168.1.1

Third Line: netsh interface ip add dns "Local Area Connection" 192.168.1.2 index=2

In this example, the first line basically states, set the wired interface "Local Area Connection" with an IP of 192.168.1.100, netmask of 255.255.255.0, gateway of 192.168.1.1 (normally your home router), and finally to give that gateway a metric of 0, meaning it's the default gateway and normally you only have one gateway on a home network. The second line is just saying for the same interface add 192.168.1.1 as the primary DNS server, and obviously the third one is just saying add 192.168.1.2 as the secondary DNS server.

An example of how to easily set an interface to DHCP from a batch file after it has been set to to static is as follows:

First Line: netsh interface ip set address "Local Area Connection" dhcp

Second Line: netsh interface ip set dns "Local Area Connection" dhcp


I figured this example basically needed no explaination but first line says dymanic IP, second says dynamic DNS.

You could easily combine both examples if for example you want to have a DHCP ip but static DNS you could use:

First Line: netsh interface ip set address "Local Area Connection" dhcp

Second Line:
netsh interface ip set dns "Local Area Connection" static 192.168.1.1

Third Line: netsh interface ip add dns "Local Area Connection" 192.168.1.2 index=2


These lines have already been explained in the above example.

If you would like to turn on and off the Windows Firewall using netsh as well the commands are as followed:

Firewall On: netsh firewall set opmode mode = enable interface = "Local Area Connection"

Firewall Off: netsh firewall set opmode mode = disable interface = "Local Area Connection"

Another cool way to use netsh when it comes to the Windows Firewall, is to use the following commands to enable or disable the firewall for every interface at once.

All Interfaces Firewall On: netsh firewall set opmode enable

All Interfaces Firewall Off: netsh firewall set opmode disable

Tip: Of course, you do not need to use these commands only with your wired network card and can use them with your wireless card by simply changing the name to "Wireless Network Connection" or whatever the name is in Network Connections in the Control Panel.

Also if you do not know how to write a batch file, you just can simply open notepad or wordpad and type in any command you wish to execute, then when you want to save the file, tell notepad or wordpad All Files instead of just *.txt or *.rtf and save the file as whatever.bat you must include .bat so that Windows knows that you have just created a batch file and that it's to run using the command prompt.

I hope that these tips and tricks can help make your life Network Administration much easier, as not only can you use these batch files, but can incorporate them in to logon scripts if you run Windows Server or even a Samba Server running on Linux configured as a Primary Domain Controller.

Happy Network Administrating forever!


No comments:

Post a Comment