Skip to main content

Antonio Cisternino's Home Page

Go Search
Home
  
Antonio Cisternino's Home Page > My Blog > Netsh... a little known command  

My Blog: Netsh... a little known command

Title

Netsh... a little known command 

Body

It is long time since my last post. I'm sorry but many things happened since then, and I try to blog when I have something to say (hopefully interesting).
Many people tend to judge Windows by the user interface, without even attempting to look in the standard interface. A nice example that shows this is provided by the netsh command: a great tool that very few people (including sysadmins) know. Besides, people claim that Windows doesn't provide support for multiple network configurations. In fact Explorer doesn't provide a graphical UI to hold multiple network configurations, but that's just UI!
Under the hood the real configuration can be controlled using the netsh command, a rather verbose command (like many MS provided) that allows configuring the network.
One of the most interesting facts about netsh is that you can dump the current networking configuration and load it.
The following command dump the configuration to a file:
 
netsh interface ip dump > conf.netsh
 
The command dumps to the standard output the configuration in a form of netsh script so that you can execute later to reset the configuration. The command to load the saved configuration is the following:
 
netsh exec conf.netsh
 
Ok, ok, I know this ísn't the best possible solution! We're used to GUIs and I'm proposing a traditional, command based interface?
So let's use the standard mechanisms of Windows Explorer to save and restore multiple network configurations.
First of all we have to create a file type associated with the .netsh extension (the one we choose to hold network configurations).
Although it is possible to associate programs with file extensions using the explorer interface (open a folder and choose Tools->Folder Options->File Types) we go for the hard way and change the registry using regedit.exe.
First of all we create the following key:
 
HKEY_CLASSES_ROOT\.netsh
 
and in the (Default) value type "netshfile".
With this entry we inform explorer that netshfile is the type associated with the .netsh extension.
So let's define the netshfile type by creating the following key:
 
HKEY_CLASSES_ROOT\netshfile
 
Set the (Default) value to "Network Settings" that is the string describing the file type.
If you edit the file type with explorer you also get the following values associated to the key:
 
"EditFlags"=dword:00000000
"BrowserFlags"=dword:00000008
 
We have to associate an Icon to the file type. So we create the following key:
 
HKEY_CLASSES_ROOT\netshfile\DefaultIcon
 
And set the (Default) value to "shell32.dll,18".
We are interested in adding operations to the file type, to do so we create the following key:
 
HKEY_CLASSES_ROOT\netshfile\shell
 
Netsh produces text scripts, so we can provide an "Edit" command that simply opens the file with notepad. Moreover when you double-click on a .netsh file we want to execute the exec command of netsh.
Shell commands are defined using subkeys of the shell key, thus we create the following subkeys:
 
HKEY_CLASSES_ROOT\netshfile\shell\edit
HKEY_CLASSES_ROOT\netshfile\shell\edit\command
 
In the (Default) value of the command key type
 
notepad.exe "%1"
 
This is the command executed when the user asks for "Edit operation" (for instance through the context menu when you right-click on a file).
In the same way we define the open command:
 
HKEY_CLASSES_ROOT\netshfile\shell\open
HKEY_CLASSES_ROOT\netshfile\shell\open\command
 
And set the (Default) value of command to:
 
netsh.exe exec "%1"
 
But what operation is used when you double-click on a .netsh file? You can set the default operation by using its name in the (Default) value of the shell key (in our case "open").
Now you can double-click on .netsh files to load the network configuration previously saved.
How can we save the current configuration? The natural way would be by having a "New -> Network Settings" in the New menu. The New menu of the shell is obtained by looking for "ShellNew" subkeys of the .xxx keys (those describing file associations). So let's create the following key:
 
HKEY_CLASSES_ROOT\.netsh\ShellNew
 
because we want to run a command when you pick the "Network settings" entry we define a REG_EXPAND_SZ value in the above key and we set it to the value
 
cmd.exe /C netsh.exe interface ip dump > "%1"
 
This command simply dumps the configuration to the given file.
Now you can dump your configuration by right-clicking on a folder and selecting "Network settings", then you can rename the new file and restore the associated configuration by double-clicking on it.
You can do all the settings by cut&paste the following REG definition and save in a .reg file and double-click on it:
 
REGEDIT4
[HKEY_CLASSES_ROOT\.netsh]
@="netshfile"
[HKEY_CLASSES_ROOT\.netsh\ShellNew]
"Command"=hex(2):63,6d,64,2e,65,78,65,20,2f,43,20,6e,65,74,73,68,2e,65,78,65,\
  20,69,6e,74,65,72,66,61,63,65,20,69,70,20,64,75,6d,70,20,3e,20,22,25,31,22,\
  00
[HKEY_CLASSES_ROOT\netshfile]
@="Network profile"
"EditFlags"=dword:00000000
"BrowserFlags"=dword:00000008
[HKEY_CLASSES_ROOT\netshfile\DefaultIcon]
@="shell32.dll,18"
[HKEY_CLASSES_ROOT\netshfile\shell]
@="open"
[HKEY_CLASSES_ROOT\netshfile\shell\edit]
[HKEY_CLASSES_ROOT\netshfile\shell\edit\command]
@="notepad.exe \"%1\""
[HKEY_CLASSES_ROOT\netshfile\shell\open]
[HKEY_CLASSES_ROOT\netshfile\shell\open\command]
@="netsh.exe exec \"%1\""
[HKEY_CLASSES_ROOT\netshfile\shell\open\ddeexec]
[HKEY_CLASSES_ROOT\netshfile\shell\open\ddeexec\Application]
@="netsh"
[HKEY_CLASSES_ROOT\netshfile\shell\open\ddeexec\Topic]
@="System"

Expires

 

Category

Software 
Attachments
Created at 2/22/2005 23:26  by Antonio Cisternino 
Last modified at 2/22/2005 23:26  by Antonio Cisternino