Click here to Skip to main content
Click here to Skip to main content
Go to top

DNS and MTU Network Configuration Tool

, 13 Jun 2009
Rate this:
Please Sign up or sign in to vote.
Tool to administer DNS and MTU configuration
Network Config

Introduction

This VB.NET console application allows you to:

  • Set the DNS server configuration for a workstation or server, the server IPs are given as arguments to the application
  • Set the MTU size for workstation or server

Background

In our environment we cannot use DHCP, Netsh will manage NIC configuration, but it is kludge and you need to specify the interface name. This tool finds the GUID for the DNS registered adapter(s) and updates the DNS servers. DnsConfig.exe may work with Vista, Windows 7 and Windows Server, but has not been tested on platforms other than XP. This tool is a good starting point for any command line tool that will take arguments and modify the Windows registry. I did not author the sub routines that modify the registry, write the log file and enumerate registry sub keys; they have been recycled.

Using the Code

Copy the tool to %systemroot%\system32

  • Argument usage:
    • dnsConfig.exe <version> <MTU> <Primary DNS>,<Secondary DNS> <log file path>
  • Examples:
    • DNS only
      • dnsConfig.exe 2 192.168.0.1,192.168.0.2 \\192.168.15.25\logs 
    • MTU only
      • dnsConfig.exe 2 MTU:1500 \\192.168.15.25\logs
    • MTU + DNS
      • dnsConfig.exe 2 MTU:1500 192.168.0.1,192.168.0.2 \\192.168.15.25\logs  

DNS server entries are not limited. The DNS server argument becomes the NameServer registry value.

' Process Command line Args
Public Function Argument(ByVal args() As String)

    ' Show help if args not correct
    ' 5 Args = DNS + MTU
    ' 4 Args = DNS only
    ' 3 Args =  MTU only
    If args Is Nothing OrElse args.Length < 4 OrElse _
    args.Length > 5 Then

        Call ShowHelp()
        Return False

    Else

        If args.Length = 5 Then

            dnsVer = args(1)
            MTU = args(2).Substring(4)
            DNS_Values = args(3)
            Log_Path = args(4)

            oprDNS = True
            oprMTU = True

        End If

        If args.Length = 4 Then

            If args(2).CompareTo("MTU:") = 1 Then
                ' setup for MTU only
                dnsVer = args(1)
                MTU = args(2).Substring(4)
                Log_Path = args(3)
                oprMTU = True

            Else
                ' setup for DNS only
                dnsVer = args(1)
                DNS_Values = args(2)
                Log_Path = args(3)
                oprDNS = True

            End If

        End If

        Return True
    End If

End Function

Argument Function

The function above handles the arguments. This function could be leveraged for any console application with a different quantity or type of input.

' Determine GUID of DNS registered adapters
' Write NameServer values from arguments
Public Function GetAllChildSubKeys(ByVal MainKey As RegistryKey, _
				ByVal sKey As String) As ArrayList
    Dim rkKey As RegistryKey    'RegistryKey to work with
    Dim sSubKeys() As String    'string array to hold the subkeys
    GetAllChildSubKeys = Nothing

    'open the given subkey
    rkKey = MainKey.OpenSubKey(sKey)

    'get all the child subkeys
    sSubKeys = rkKey.GetSubKeyNames
    'loop through all the child subkeys
    For Each s As String In sSubKeys

        If oprDNS = True Then
            ' Update NameServer Values in Registry
            Call MyRegistry(Registry.LocalMachine, _
		"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" _
		+ (s), "NameServer", DNS_Values, "W", RegistryValueKind.String)

        End If

        If oprMTU = True Then

            ' Update NameServer Values in Registry
            Call MyRegistry(Registry.LocalMachine, _
		"SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" _
		+ (s), "MTU", MTU, "W", RegistryValueKind.DWord)

        End If
    Next

    ' Write Log if Multiple adapters registered for DNS
    If sSubKeys.Length() > 1 Then
        Dim strComputerName As String
        strComputerName = Environment.GetEnvironmentVariable("ComputerName")
        Call WriteData("ERROR -- Multiple DNS registered adapters", _
		Log_Path, Log_Path + "\" + strComputerName + ".txt", True)

    End If
End Function

Sub Key Function

The function above builds a collection of sub keys and updates the DNS and MTU values depending on the arguments provided:

 'Write or read registry value(s)

    Public Function MyRegistry(ByVal MainKey As RegistryKey, ByVal sKey As String, _
               ByVal Value As String, ByVal Data As String, ByVal RorW As String, _
               ByVal RegistryValueKind As RegistryValueKind)

        Dim regKey As RegistryKey
        MyRegistry = Nothing
        regKey = MainKey.CreateSubKey(sKey)

        If RorW = "W" Then
            regKey.SetValue(Value, Data, RegistryValueKind)
            regKey.Close()
        End If

        If RorW = "R" Then
            MyRegistry = regKey.GetValue(Value)
            regKey.Close()
        End If

    End Function

The function above is used to read or write to/from the Windows registry.

It allows control of the value type created:

  • String Value
  • Binary Value
  • Dword Value
  • Multi-String Value
  • Expandable String Value

Example

MyRegistry(<Hive>,<Registry Key>,<Value Name>,<Data>,<Operation>,<Value Type>) 

MyRegistry(Registry.LocalMachine, _
"SYSTEM\ControlSet001\Services\Tcpip", "dnsVer", "3.5", "W", RegistryValueKind.String) 

Points of Interest

.NET has security restrictions that prevent console applications from being executed across the network; the application needs to reside on the local system. My original intention was to call the application with group policy and pass the arguments from group policy. Due to the security restrictions I ended up with a Vbscript in group policy that copies the application to the workstation and then calls it with arguments. Of course, this could all have been done in Vbscript, PowerShell, etc., but I want to learn VB.NET. Visual Developer 2008 is so efficient it has removed the time barrier for compiling code, with compiled admin tools you don't have to worry about another admin altering the code or administering signed scripts, etc.

History

  • 11th June, 2009: Initial post on CodeProject
  • 12th June, 2009: Updated article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Jeff Cator
Engineer Van Dyke Technology Group
United States United States
No Biography provided

Comments and Discussions

 
QuestionDoes the script only work once per computer? PinmemberShawn Welker13-Aug-09 11:20 
AnswerRe: Does the script only work once per computer? [modified] PinmemberJeff Cator9-Dec-09 11:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 13 Jun 2009
Article Copyright 2009 by Jeff Cator
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid