Avatar billede qbob Nybegynder
29. juni 2004 - 12:21 Der er 1 kommentar og
1 løsning

At tilføje mange domainer på en Win2000 DNS server.

Hej dig.

Jeg er lige blevet varslet at firmaet skal til og købe en 150 domaines snart.
Det giver mig lidt sved på panden da det er mig der skal tilføje dem i vores dns server.

Nogle der har en smart måde og gøre det på / et lille program der kan impotere efter en text fil eller andre løsnings forslag, som ikke gør at jeg skal bruge en dag på det ?

(Alle domainer skal bare have en C-NAME til firma.dk)
Avatar billede strych9 Praktikant
13. juli 2004 - 23:36 #1
mmm jeg vil egentlig nok advare dig imod at køre internet dns på windows.. Hvis du eventuelt skifter til noget bedre så som BIND på Linux så skal jeg nok lave et script til dig som opretter de 150 domæner der ;)
Avatar billede -mundi- Nybegynder
13. juli 2004 - 23:37 #2
måske kan du bruge det her ?

gem det som .vbs

'bdns_wmi.vbs*
'**
'* This script is designed to facilitate the administrative routines of creating
'* primary and secondary zones on DNS servers running Windows2000 (or NT4.0 with
'* WMI Core 1.5 installed), as well as adding records to existing zones.
'* Naturally, you will need administrative rights on the DNS servers you call.
'* You will also need the WMI DNS Provider (dnsprov.dll), which is included in
'* Windows 2000 Resource Kit Supplement 1 and can be downloaded for free from
'* ftp://ftp.microsoft.com/reskit/win2000/dnsprov.zip
'* The provider must be installed on every DNS server you will manage.
'**
'* If you have any questions or comments, please mail me at sezel@metric.ru
'**
'***

Dim objLocator,objPService,objSService,objPServer,objSServer
Dim strPServer,strSServer,strZoneName,strPrimIP

'*GLOBAL VARIABLES***
'* objLocator - the SWBEMLocator object;
'* objPService - the SWBEMServices object, bound to the \microsoftdns namespace
'* of the primary DNS server you specify;
'* objSService - the SWBEMServices object, bound to the \microsoftdns namespace
'* of the secondary DNS server you specify;
'* objPServer - the primary DNS server object;
'* objSServer - the secondary DNS server object;
'* strPServer - stores name of the primary DNS server;
'* strSServer - stores name of the secondary DNS server;
'* strZoneName - stores name of the zone you want to create or modify;
'* strPrimIP - stores the first IP the primary server uses for listening
'* on NS requests. Required for automatic creation of secondary
'* zones
'***

Const ZONE_ADMIN = "admin.yourdomain.com" ' Specify the 'Responsible person' part of
                                          ' SOA record for the zones you create
Const REFRESH_INTERVAL = 10800 'All these constants are zone parameters,
Const RETRY_DELAY = 1800 'contained in the SOA record. Modify them
Const EXPIRE_LIMIT = 604800 'as better suits your needs.
Const MINIMUM_TTL = 21600

'Create Locator object to connect to remote CIM object manager:
On Error Resume Next
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
If Not ErrorOccurred(Err,"creating locator object.") Then
  Call SpecAction()
End If

'***
'* The following subroutine receives initial information about what actions the
'* caller intends to perform. Different subroutines are called depending on user
'* input.
'***

Sub SpecAction()
On Error Resume Next
    'Check to see if it is not the first time the sub was called and the zone name was
    'already specified:
    If strZoneName = "" Then
        'Prompt if user wants to create a primary zone:
        intCrPrim = MsgBox("Create primary zone?",36,"Create Zone")
        'If not, may be they want to add records to existing zone:
        If intCrPrim = 7 Then
            intAddRec = MsgBox("Add records to existing zone?",36,"Edit Zone")
        End If
        'In either case, prompt for primary DNS server name:
        If intCrPrim = 6 Or intAddRec = 6 Then
            'Enter a loop that can be exited either by entering valid data or cancelling script operation:
            Do
              strPServer = Trim(InputBox("Enter FQN of your primary DNS server","Create Zone"))
              If strPServer = "" Then
                  Exit Sub
              'Validate the name entered, using a special function defined below:
              ElseIf Validate(strPServer,"fqn") Then
                  Exit Do
              Else
                  MsgBox "Name specified is invalid!",48,"Create Zone"
              End If
            Loop
            'Connect to the primary DNS server, using the ConnectServ function described below.
            'Empty values stand for logon creadentials (by default connection is attempted with the
            'creadentials of the user currently logged on):
            ConnectServ strPServer,"","",objPService
                'The function is supposed to return a SWBEMServices object.
                'If it doesn't, quit (error message is displayed by function itself):
                If Not IsObject(objPService) Then
                    Exit Sub
                Else
                    'Obtain the primary DNS server object:
                    Set objPServer = objPService.Get("MicrosoftDNS_Server.name="".""")
                      'Handle any error that may occur, using a custom error-handling function:
                      If ErrorOccurred(Err,"connecting to server " & strPServer) Then
                          Exit Sub
                      End If
                    'Initialize the global variable:
                    strPrimIP = objPServer.ListenIPAddressesArray(0)
                      'If the variable is empty (this will happen in case your primary DNS server
                      'is configured to listen on all addresses), then get IP by implementing another method:
                      If strPrimIP = "" Then
                          strPrimIP = objPServer.ServerIPAddressesArray(0)
                      End If
                End If
        End If
    End If
    'If only user did not choose to add records to existing zone, prompt if secondary
    'zone needs to be created:
    If intAddRec <> 6 Then
        intCrSec = MsgBox("Create secondary zone?",36,"Create Zone")
    End If
    'Prompt for secondary server name:
    If intCrSec = 6 Then
        Do
          strSServer = Trim(InputBox("Enter FQN of your secondary DNS server","Create Zone"))
              If strSServer = "" Then
                Exit Sub
              ElseIf Validate(strSServer,"fqn") Then
                Exit Do
              Else
                MsgBox "Name specified is invalid!",48,"Create Zone"
              End If
        Loop
        'Connect to the secondary DNS server, using the ConnectServ function described below:
        ConnectServ strSServer,"","",objSService
            If Not IsObject(objSService) Then
                Exit Sub
            Else
                Set objSServer = objSService.Get("MicrosoftDNS_Server.name="".""")
                  If ErrorOccurred(Err,"connecting to server " & strSServer) Then
                      Exit Sub
                  End If
            End If
    End If
    'If the primary DNS server was specified and successfully bound, then prompt for action:
    If IsObject(objPService) Then
        If intCrPrim = 6 Then
            'Call a subroutine to create a new zone:
            CreateNewZone
        ElseIf strZoneName <> "" Then
            'Call a subroutine to create a new zone.
            'This condition will be met in case the user initially wanted to add records to
            'a non-existing zone, and when notified chose to create one:
            CreateNewZone
        ElseIf intAddRec = 6 Then
            'Call a subroutine to add records to an existing zone:
            EditZone
        End If
    Else
        'If the secondary server only was specified, then call a subroutine
        'to create a secondary zone:
        If IsObject(objSService) Then
            CreateSecondary(1) ' the value of 1 indicates that the secondary zone is created
                              ' "separately", i.e. the address of primary server needs to be
                              ' specified explicitly
        End If
    End If
End Sub

'***
'* This is the main subroutine that creates a standard primary zone.
'***

Sub CreateNewZone
On Error Resume Next
    'If zone name was not specified before (e.g. in the EditZone sub), prompt user for it:
    If strZoneName = "" Then
      Do
          strZoneName = Trim(InputBox("Insert name of zone to be" & vbNewLine & "created (e.g. newzone.com)", _
          "Create new zone on " & strPServer))
            If strZoneName = "" Then
                Exit Sub
            ElseIf Validate(strZoneName,"fqn") Then
                Exit Do
            Else
                MsgBox "Name specified is invalid!",48,"Create Zone"
            End If
      Loop
    End If
'Try to bind this zone to see if it already exists:
Set objDNSset = objPService.Get("MicrosoftDNS_Zone.ContainerName=""" & strZoneName & """,DnsServerName=""" & objPServer.Name & """,Name=""" & strZoneName & """")
    If Err.Number = 0 Then
        MsgBox "Zone " & strZoneName & " already exists!", 48, "Create new zone on " & strPServer
        Exit Sub
    End If
Err.Clear

'Create an instance of the MicrosoftDNS_ZONE class:
Set objDNSSet = objPService.Get("MicrosoftDNS_ZONE")

'Call the CreateZone method, specifying zone name and type (1=Standard Primary):
oNewZone = objDNSSet.CreateZone(strZoneName,1)
    If ErrorOccurred(Err,"attempting to create zone.") Then
        Exit Sub
    End If

'Obtain SOA record class instance for the newly created zone:
Set oSOA = objPService.ExecQuery("select from MicrosoftDNS_SOAType where DomainName=""" & strZoneName & """")
    For Each SOA in oSOA 'oSOA collection should always contain one item only
        'Modify SOA record using declared constants and Version function:
        SOA.Modify,Version,strPServer,ZONE_ADMIN,REFRESH_INTERVAL,RETRY_DELAY,EXPIRE_LIMIT,MINIMUM_TTL
          If ErrorOccurred(Err,"modifying SOA record" & vbNewLine & "for zone " & strZoneName) Then
              Exit Sub
          End If
    Next

'Obtain NS record class instance that binds to primary NS record:
Set oNS = objPService.ExecQuery("select from MicrosoftDNS_NSType where DomainName=""" & strZoneName & """")
    'It may happen so under certain circumstances that the name of the primary NS server in the zone you create
    'will be different from the name you specify and the one that is registered with the delegating authority.
    'So it is better to double-check and set this name explicitly:
    For Each NS in oNS    'oNS collection should always contain one item only
      If NS.NSHost <> strPServer Then
          NS.Modify ,strPServer
            If ErrorOccurred(Err,"setting primary NS server value for zone " & vbNewLine & _
            strZoneName & " to " & strPServer) Then
                Exit Sub
            End If
      End If
    Next

'Bind the newly created zone to change some properties:
Set oZone = objPService.Get("MicrosoftDNS_Zone.ContainerName=""" & strZoneName & _
""",DnsServerName=""" & objPServer.Name & """,Name=""" & strZoneName & """")
  If ErrorOccurred(Err,"binding zone " & strZoneName) Then
      Exit Sub
  End If
  'If secondary server name was specified, then create NS record for this server:
  If strSServer <> "" Then
      Set oNS = objPService.Get("MicrosoftDNS_NSType")
      oNS.CreateInstanceFromPropertyData strPServer,strZoneName,strZoneName,,,strSServer
        If ErrorOccurred(Err,"adding secondary NS server" & vbNewLine & strSServer) Then
            Exit Sub
        End If
  End If
'Call the ResetSecondaryIpArray method to set the zone to allow transfers to configured NS servers only
'and to notify those servers of zone modifications:
oZone.ResetSecondaryIpArray ,1,,1
  If ErrorOccurred(Err,"attemtpting to change " & vbNewLine & "Zone Transfer settings") Then
      Exit Sub
  End If
'Call subroutine that adds MX records to zone:
CreateMX(Empty)
'Call routine that adds "ftp" A record to zone:
CheckFTP
End Sub

'***
'* The following subroutine is designed for adding MX records to zone. Its only
'* input parameter is an integer that is used to skip the first prompt when it is
'* not empty.
'***

Sub CreateMX(intSetMX)
On Error Resume Next
    Do
      'Seed the value that will be used as default in the 'Specify Priority' input
      'box (and be incremented with each loop return):
      intPriority = intPriority + 5
          'If this value is greater than seed, it means at least one record has already been created:
          If intPriority > 5 Then
            strAnother = "another "
          End If
          'If parameter was not passed, prompt user if MX record should be created:
          If IsEmpty(intSetMX) Then
            intSetMX = MsgBox("Create " & strAnother & "MX record?",36,"Create MX record")
          End If
          If intSetMX = 7 Then
            Exit Do
          Else
                'Prompt for mail server name:
                Do
                  strMailServer = Trim(InputBox("Enter name of mail server:","Mail Server"))
                      If strMailServer = "" Then
                        Exit Sub
                      ElseIf Validate(strMailServer,"fqn") Then
                        Exit Do
                      Else
                        MsgBox "Name specified is invalid!",48,"Add MX Record"
                      End If
                Loop
                'Prompt for mail server priority:
                Do
                  intPriority = Trim(InputBox("Enter mail server priority:","Priority",intPriority))
                      If IsNumeric(intPriority) Then
                        Exit Do
                      ElseIf intPriority = "" Then
                        Exit Sub
                      Else
                        MsgBox "Value specified is invalid!",48,"Add MX Record"
                      End If
                Loop
            'If it is the first time the block is parsed, create a MicrosoftDNS_MXType class instance:
            If Not IsObject(objMX) Then
                Set objMX = objPService.Get("MicrosoftDNS_MXType")
            End If
            'Create MX record
            objMX.CreateInstanceFromPropertyData strPServer,strZoneName,strZoneName,,,intPriority,strMailServer
                If ErrorOccurred(Err,"adding MX record " & strMailServer) Then
                  Exit Do
                End If
            intSetMX = Empty
          End If
    Loop
End Sub

'***
'* The following subroutine is designed for adding an A record for host "ftp" in
'* the process of zone creation. It also stores the address entered for this
'* record and passes it to another subroutine.
'***

Sub CheckFTP
'Prompt user if A record for host 'ftp' should be created:
intSetFtp = MsgBox("Create record for host ftp." & strZoneName & "?",36,"Create A Record")
    If intSetFtp = 6 Then
        Do
          strFtpHost = Trim(InputBox("Enter IP address for host" & vbNewLine & "ftp." & strZoneName,"FTP"))
              If strFtpHost = "" Then
                Exit Sub
              ElseIf Validate(strFtpHost,"ip") Then
                Exit Do
              Else
                MsgBox "Address specified is invalid!",48,"Add A Record"
              End If
        Loop
          'Call the CreateA function to create record; exit if the function fails:
          If CreateA("ftp",strFtpHost) Then
              Exit Sub
          End If
        CheckWWW strFtpHost
    Else
        CheckWWW ""
    End If
End Sub

'***
'* The following subroutine is designed for adding an A record for host "www" in
'* the process of zone creation. It is called from the CheckFTP function and
'* takes one parameter as input: address of the 'ftp' A-record (empty if it was
'* not created)
'***


Sub CheckWWW(strFtpHost)
On Error Resume Next
'Prompt user if A record for host 'www' should be created:
intSetWWW = MsgBox("Create record for host www." & strZoneName & "?",36,"Create A Record")
  If intSetWWW = 6 Then
      If strFtpHost <> "" Then
        'If the A-record for host 'ftp' was created in the previous step, prompt user if the same address
        'should be used for 'www':
        intUseSame = MsgBox("Use the same IP-address as for ftp." & strZoneName & "?",36,"Create A Record")
        If intUseSame = 6 Then
            strWWWHost = strFtpHost
        End If
      End If
      If strWWWHost = "" Then
        Do
            strWWWHost = Trim(InputBox("Enter IP address for host" & vbNewLine & "www." & strZoneName,"WWW"))
              If strWWWHost = "" Then
                  Exit Sub
              ElseIf Validate(strWWWHost,"ip") Then
                  Exit Do
              Else
                  MsgBox "Address specified is invalid!",48,"Add A Record"
              End If
        Loop
      End If
      'Call the CreateA function to create record; exit if the function fails:
      If CreateA("www",strWWWHost) Then
        Exit Sub
      End If
  End If
  'If secondary server was specified, call function that will create a copy of the zone on the secondary server:
  If strSServer <> "" Then
      CreateSecondary(0)
  Else
      'Else prompt user if they want to create another zone. The parameter value stands for "primary":
      AskForAnother("p")
  End If
End Sub

'***
'* The following subroutine is designed for creating a copy of either the newly
'* created or any other zone on the specified secondary server. Its input
'* parameter is boolean in its nature, specifying whether a replica is being
'* created following the creation of a primary zone or it is created "separately"
'***


Sub CreateSecondary(sep)
On Error Resume Next
    'Get an instance of the MicrosoftDNS_ZONE class:
    Set objDNSSet = objSService.Get("MicrosoftDNS_ZONE")
        If sep = 1 Then
            'It is a new seconday zone, not a copy of the primary zone we just created,
            'so user must be prompted for name:
            Do
              strSecName = Trim(InputBox("Enter name of zone to be" & vbNewLine & "created (e.g. newzone.com)", "Create a secondary zone on " & strSServer))
                  If strSecName = "" Then
                    Exit Sub
                  ElseIf Validate(strSecName,"fqn") Then
                    Exit Do
                  Else
                    MsgBox "Name specified is invalid!",48,"Create Zone"
                  End If
            Loop
            'Specify name of the primary DNS server for the zone to be transferred from:
            Do
              strPrimary = Trim(InputBox("Specify IP-address of the primary DNS-server","Create a secondary zone on " & strSServer))
                  If strPrimary = "" Then
                    Exit Sub
                  ElseIf Validate(strPrimary,"ip") Then
                    strIP = strPrimary
                    Exit Do
                  Else
                    MsgBox "Address specified is invalid!",48,"Create Zone"
                  End If
            Loop
            'Initialize variable to indicate that a secondary zone only was created,
            'when prompting user whether he wants to create another:
            strCall = "s"
        Else
            'If the secondary zone is a replica of the zone we just created on the primary server,
            'then we know both its name and the IP of the primary server:
            strSecName = strZoneName
            strIP = strPrimIP
            strCall = "p"
        End If
'Call the CreateZone method, specifying zone name and type (2=Standard Secondary), as well as address
'of the primary DNS server:
oNewZone = objDNSSet.CreateZone(strSecName,2,strSecName & ".dns",Array(strIP))
    If Not ErrorOccurred(Err,"attempting to create secondary zone " & strSecName) Then
        'If everything is fine, prompt user if another zone needs to be created:
        AskForAnother(strCall)
    End If
End Sub

'***
'* This small subroutine is used to check whether user wnats to create another
'* zone or just quit. A user will only be presented with the choice if a new zone
'* was actually created (if you only edited zones, the script must be re-run to
'* edit another)
'* The input parameter indicates the type of zone that was created.
'***


Sub AskForAnother(strType)
intMakeAnother = MsgBox("Zone created successfully!" & vbNewLine & "Create another new zone?",36,"Create a new zone")
    If intMakeAnother = 6 Then
      If strType = "p" Then
          CreateNewZone
      ElseIf strType = "s" Then
          CreateSecondary(1)
      End If
    End If
End Sub

'***
'* This subroutine is used to add records to existing zones, rather than creating
'* new ones.
'***

Sub EditZone
On Error Resume Next
    If strZoneName = "" Then
      'Prompt for zone name:
        Do
          strZoneName = Trim(InputBox("Enter name of zone you want to edit", "Editing zone on " & strSServer))
              If strZoneName = "" Then
                Exit Sub
              ElseIf Validate(strZoneName,"fqn") Then
                Exit Do
              Else
                MsgBox "Name specified is invalid!",48,"Create Zone"
              End If
        Loop
        'Get a zone object, bound to the specified zone:
        Set objDNSset = objPService.Get("MicrosoftDNS_Zone.ContainerName=""" & strZoneName & _
        """,DnsServerName=""" & objPServer.Name & """,Name=""" & strZoneName & """")
            'If the object could not be bound, presume that zone does not exist
            'and prompt user if a zone with such name should be created:
            If Err.Number <> 0 Then
                intNoZone = MsgBox("Zone " & strZoneName & " does not exist!" & _
                vbNewLine & "Create zone?",52, "Creating a new zone on " & strPServer)
                  If intNoZone = 6 Then
                      Err.Clear
                      SpecAction
                  Else
                      Exit Sub
                  End If
            End If
    End If
    strRecType = UCase(Trim(InputBox("Enter record type:" & vbNewLine & _
    "(A, CNAME or MX)", "Editing zone " & strZoneName)))
    'Get information from user depending on the type of record to be created:
    Select Case strRecType
        Case "A"
          Do
              strRecName = Trim(InputBox("Enter record name (relative):","Editing zone " & strZoneName))
                If strRecName = "" Then
                    Exit Sub
                ElseIf Validate(strRecName,"host") Then
                    Exit Do
                Else
                    MsgBox "Name specified is invalid!",48,"Create Zone"
                End If
          Loop
          Do
              strRecData = Trim(InputBox("Enter IP address for record" & vbNewLine & _
              strZoneName & Chr(46) & strRecName,"Editing zone " & strZoneName))
                If strRecData = "" Then
                    Exit Sub
                ElseIf Validate(strRecData,"ip") Then
                    Exit Do
                Else
                    MsgBox "Address specified is invalid!",48,"Create Zone"
                End If
          Loop
          'Create A record by calling a function defined below:
          CreateA strRecName,strRecData
        Case "CNAME"
          Do
              strRecName = Trim(InputBox("Enter alias name (relative):","Editing zone " & strZoneName))
                If strRecName = "" Then
                    Exit Sub
                ElseIf Validate(strRecName,"host") Then
                    Exit Do
                Else
                    MsgBox "Name specified is invalid!",48,"Create Zone"
                End If
          Loop
          Do
              strRecData = Trim(InputBox("Enter FQN for alias " & _
              strRecName & Chr(46) & strZoneName,"Editing zone " & strZoneName))
                If strRecData = "" Then
                    Exit Sub
                ElseIf Validate(strRecData,"fqn") Then
                    Exit Do
                Else
                    MsgBox "Name specified is invalid!",48,"Create Zone"
                End If
          Loop
              'Check if an instance of CNAMEType record was previously created
              '(in case it is not the first time the subroutine is called):
              If Not IsObject(objCNAME) Then
                Set objCNAME = objPService.Get("MicrosoftDNS_CNAMEType")
              End If
              'Implement the CreateInstanceFromPropertyData method to create a CNAME record in the zone:
              objCNAME.CreateInstanceFromPropertyData strPServer,strZoneName,strRecName & Chr(46) & strZoneName,,,strRecData
              'Call the error-handling function to display error message, if any:
              ErrorOccurred Err,"creating CNAME record " & strRecName & Chr(46) & strZoneName
        'Call the CheckMX function defined above to create MX records, in case user chose so:
        Case "MX"
          CheckMX(6)
        Case ""
          Exit Sub
        'The script will get here if user entered something other than "A", "MX" or "CNAME":
        Case Else
          MsgBox "Wrong record type specified!",16,"Editing zone " & strZoneName
    End Select
    'Prompt user if another record needs to be created:
    intAddAnother = MsgBox("Add another record?",36,"Editing zone " & strZoneName)
      If intAddAnother = 6 Then
          EditZone
      Else
          Exit Sub
      End If
End Sub

'***
'* The following function is used to connect to the specified DNS server. It
'* takes server name, login credentials and object handler as input and returns a
'* SWBEMServices object bound to the \microsoftdns namespace of the server.
'***

Function ConnectServ(strServer,strLogin,strPass,objService)
On Error Resume Next
    'If no credentials were passed, attempt to connect with the caller's credentials.
    'This check is required since connection attempt with emtpy login and password
    'would cause an error:
    If strLogin = "" Then
        Set objService = objLocator.ConnectServer(strServer,"root\microsoftdns")
    Else
        Set objService = objLocator.ConnectServer(strServer,"root\microsoftdns",strLogin,strPass)
    End If
    'In case of error first check if the error is "Access denied"
    'If this is so, then prompt the user for login and password and call
    'the function again:
    If Err.Number Then
        If InStr(Err.Description,"Access is denied") <> 0 Then
            Err.Clear
            strLogin = Trim(InputBox("Enter login to access server " & strServer,"Create Zone"))
                If strLogin = "" Then
                    ConnectServ = True
                    Exit Function
                End If
            strPass = Trim(InputBox("Enter password for user " & strLogin,"Create Zone"))
                If strPass = "" Then
                    Exit Function
                End If
            ConnectServ = ConnectServ(strServer,strLogin,strPass,objService)
        Else
            ErrorOccurred Err,"connecting to server " & strServer
        End If
    Else
        'If the server was bound successfully, set the impersonation level to 'Impersonate':
        objService.Security_.impersonationlevel = 3
    End If
End Function

'***
'* This small function is used to create A records in zones. Its input parameters
'* are name of the record to be created and its IP address. The function returns
'* TRUE in case of an error, and FALSE in case a record was successfully created.
'***

Function CreateA(strRName,strRData)
On Error Resume Next
CreateA = False
Set objA = objPService.Get("MicrosoftDNS_AType")
objA.CreateInstanceFromPropertyData strPServer,strZoneName,strRName & Chr(46) & strZoneName,,,strRData
  If ErrorOccurred(Err,"creating A record " & strRName) Then
      CreateA = True
  End If
End Function

'***
'* The following function validates relative and absolute DNS names, as well as IP
'* addresses for compliance with RFC requirements. It returns TRUE in case string
'*passed is valid, and FALSE if it is not.
'***

Function Validate(strToVal,stype)
  'Create new Regular Expression:
  Set regEx = New RegExp
      Select Case stype
        Case "ip"
            'Create pattern to validate IPs against:
            regEx.Pattern = "[^0-9.]"
        Case "fqn"
            'Create patternt to validate fully-qualified names against:
            regEx.Pattern = "[^a-zA-Z0-9.-]"
        Case "host"
            'Create a pattern to validate relative names.
            'It is basically the same as the pattern above, but dot is excluded:
            regEx.Pattern = "[^a-zA-Z0-9-]"
      End Select
  regEx.Global = True
  Set Matches = regEx.Execute(strToVal)
      If Matches.Count > 0 Then
        Validate = False
      Else
        Validate = True
      End If
  'Neither names nor IP-addresses can begin with a dot or have double dots:
  If Left(strToVal,1) = Chr(46) Then
      Validate = False
  ElseIf InStr(strToVal,"..") > 0 Then
      Validate = False
  End If
  'Nothing but fully-qualified names (FQNs) can end with a dot:
  If stype <> "fqn" Then
      If Right(strToVal,1) = Chr(46) Then
        Validate = False
      End If
  End If
  'A relative DNS name can not exceed 63 characters:
  If stype = "host" Then
      If Len(strToVal) > 63 Then
        Validate = False
      End If
  End If
  'Check the validity of each octet of IP address:
  If Validate = True Then
      If stype = "ip" Then
        arrOctets = Split(strToVal,Chr(46),-1,1)
            If arrOctets(0) = 0 Or arrOctets(3) = 0 Then
              Validate = False
            End If
            If Validate = True Then
              For i = 0 To 3
                  If arrOctets(i) < 0 Or arrOctets(i) > 254 Then
                    Validate = False
                  End If
              Next
            End If
      End If
  End If
End Function

'***
'* This is a globally used error-handling function, which takes two input
'* parameters: the intrinsic Err object and a custom error string, supplied by the
'* calling function or sub, which is then added to the general error message.
'* In case of a run-time error, an error message is displayed and the function
'* returns TRUE. Else, it returns FALSE.
'***


Function ErrorOccurred(Err,strErr)
    If Err.Number Then
        ErrorOccurred = True
        strErrMsg = "Error occurred " & strErr & _
        vbNewLine & "Error number: 0x" & CStr(Hex(Err.Number))
        If Err.Description <> "" Then
            strErrMsg = strErrMsg & vbNewLine & "Description: " & _
            Err.Description
        End If
        MsgBox strErrMsg,16,"Basic DNS"
        Err.Clear
    Else
        ErrorOccurred = False
    End If
End Function

'***
'* This function generates valid initial values for zone serial numbers, using the
'* following format, suggested by RFC: YYYYMMDD00
'***

Function Version
intMonthIn = Month(Date)
    If intMonthIn < 10 Then
        strMonthOut = "0" & intMonthIn
    Else
        strMonthOut = intMonthIn
    End If
intDayIn = Day(Date)
    If intDayIn < 10 Then
        strDayOut = "0" & intDayIn
    Else
        strDayOut = intDayIn
    End If
Version = Year(Date) & strMonthOut & strDayOut & "00"
End Function
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester