Tuesday, 11 August 2015

Monitoring DNS with a vbscript

DNS on windows usually come with Active directory service. But also can used to query external Domain name as long as the server have internet connection.


Hostname entries in DNS also rarely changed and some use local cache with TTL from server. Somehow, we need to monitor a change in DNS query result with alert when these changes happend.

Let say it is other Domain name which not in our control, we want to know immidiately when the Address changed in DNS. And also alert with email that the query result changed with the query info inside the email.

Within windows 7 as client, we can do this using a VBScript and a task scheduler, and access to SMTP mail server to sent out the alert.

Below are the script to be used. The script will query the domain to be monitor, and check if the result as expected. then it will sent email with the query result, and change subject if host name is changed for the monitored domain.

strEmailTo="anyong@superbadmin.com"
strEmailFrom="dnsmonitor@superbadmin.com"
strDomain="superdomain.com"
strDNSServer="192.168.1.111"
strSMTPServer="192.168.1.232"
iSMTPPort=25
strLogs = "c:\scripts\dnsmon\logs\"
Const ForAppending = 8 'for loggin
Dim strLogFile, objFSO, strDate, objLogFile

arrRecords = Array( _
 "Name:    " & strDomain ,_
 "Address:  " &  "172.10.120.121" _
)

Set objShell = CreateObject("WScript.Shell")
'Set objNSLookup = objShell.Exec("nslookup -q=MX " & strDomain & " " & strDNSServer)
Set objNSLookup = objShell.Exec("nslookup " & strDomain & " " & strDNSServer)
Set objStdOut = objNSLookup.StdOut
strOutput = objStdOut.ReadAll

strMsg = "NSLookup Output: " & VbCrLf & VbCrLf & strOutput & VbCrLf & "Test Results:" & vbCrLf

bChanged = False
For Each strRecord In arrRecords
 bRecordChanged = False
 If InStr(strOutput, strRecord) = 0 Then
  bRecordChanged = True
  bChanged = True
 End If
 strMsg = strMsg & VbCrLf & "Changed: " & bRecordChanged & " - " & strRecord
Next

strMsg = strMsg & VbCrLf & VbCrLf & "Overall result of change: " & bChanged & VbCrLf & "############################"

'Logging Part
strLogFile = strLogs & "superdomain.com-check-logs-" & Year(strDate) & "-" & Month(strDate) & "-" & Day(strDate) & ".log" 'for log file
set objFSO = CreateObject("Scripting.FileSystemObject")
set objLogFile = objFSO.OpenTextFile(strLogFile, ForAppending, True)
objLogfile.WriteLine Now & " ## " &strMsg 
objLogFile.Close
set objLogFile = Nothing
set objFSO = Nothing
'//////////
'end logging

'WScript.Echo strMsg

'Context.LogMessage strMsg
'bChanged = True
'If bChanged = True then
If True then
 Set objEmail = CreateObject("CDO.Message")
 objEmail.From = strEmailFrom
 objEmail.To = strEmailTo
 if bChanged = True then
  objEmail.Subject = "[Mon] superweb.com DNS Changed"
 else
  objEmail.Subject = "[Mon] superweb.com DNS Change"
 End If
 objEmail.Textbody = "Checked : " & Now & VbCrLf & strOutput
 objEmail.Configuration.Fields.Item _
     ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 objEmail.Configuration.Fields.Item _
     ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
 objEmail.Configuration.Fields.Item _
     ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = iSMTPPort
 objEmail.Configuration.Fields.Update
 objEmail.Send
 'Context.SetResult = 1
Else
 'Context.SetResult = 0
End If

This simple vbscript can be executed on windows and need have access to smtp server to sent out emails.


0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More