Click here to Skip to main content
6,822,613 members and growing! (21,173 online)
Email Password   helpLost your password?
General Programming » Internet / Network » Utilities     Beginner License: The Code Project Open License (CPOL)

DNS and MTU Network Configuration Tool

By Jeff Cator

Tool to administer DNS and MTU configuration
VB, Windows (WinXP), .NET, ASP.NET, Dev
Revision:4 (See All)
Posted:11 Jun 2009
Updated:13 Jun 2009
Views:4,098
Bookmarked:6 times
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
4 votes for this article.
Popularity: 2.32 Rating: 3.86 out of 5

1

2
2 votes, 50.0%
3
1 vote, 25.0%
4
1 vote, 25.0%
5
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)

About the Author

Jeff Cator


Member

Occupation: Engineer
Company: Van Dyke Technology Group
Location: United States United States

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 2 of 2 (Total in Forum: 2) (Refresh)FirstPrevNext
GeneralDoes the script only work once per computer? PinmemberShawn Welker12:20 13 Aug '09  
GeneralRe: Does the script only work once per computer? [modified] PinmemberJeff Cator12:03 9 Dec '09  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

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

PermaLink | Privacy | Terms of Use
Last Updated: 13 Jun 2009
Editor: Deeksha Shenoy
Copyright 2009 by Jeff Cator
Everything else Copyright © CodeProject, 1999-2010
Web21 | Advertise on the Code Project