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:
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:
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:
We have to associate an Icon to the file type. So we create the following key:
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:
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:
In the (Default) value of the command key type
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:
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:
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:
@="netsh.exe exec \"%1\""