Verify network translation

Again, some earlier code.  I’m not sure how good this works…I had ran it for a few weeks awhile back.  However, my IP address changed and I received a lot of notifications before I had a chance to resolve it.  The script would be better if it contained some sort of logic so that it only sent one notification.

Monday, 08 October 2007

  • The purpose of this script is to:
  • lookup a domain name with nslookup, returning an IP address
  • find the external network address translation (NAT) using whatismyip.org
  • compare the two IP addresses
  • send a notification if they are not the same

This script was designed to run as a scheduled task and in my scenario it runs every 30 minutes.  The purpose is to work similiar to the www.no-ip.com service that will dynamically update DNS based on your external IP address.  Since I do not typically have a scripting interface into my hosted external DNS, I use a simple CDO email to let me know when DNS needs to be manually updated.


NatName = "publicnat.domainname.com"
NotifyContact = " scripting.user@domainname.com"
smtpserver = "smtp.domainname.com"

IP_in_DNS = DNSLookup(NatName)
IP_on_OutSide = ExternalIP()

If Not IP_in_DNS = IP_on_OutSide Then
SendEmail NotifyContact,NatName & " issue","The network address translation for " & NatName & " (IP_" & IP_in_DNS & ") does not match the current external address of IP_" & IP_on_OutSide & "."
End If

msgbox "Done."

Function DNSLookup(sDNSName)
On error resume next

Const OpenAsDefault = -2
Const FailIfNotExist = 0
Const ForReading = 1

Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
sTemp = oShell.ExpandEnvironmentStrings("%TEMP%")
sTempFile = sTemp & "\" & oFSO.GetTempName

oShell.Run "%comspec% /c nslookup " & sDNSName & ">" & sTempFile, 0, True

Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault)
sResults = fFile.ReadAll
fFile.Close
oFSO.DeleteFile (sTempFile)

If InStr(sResults, "Address:") Then
aNameTemp = Split(sResults, "Address:")
aName = Split(Trim(aNameTemp(2)), Chr(13))
'aNameTemp(2) will be the second address listed; the first address is that of the DNS Server

DNSLookup = aName(0)
Else
DNSLookup = "Failed."
End If

Set oShell = Nothing
Set oFSO = Nothing
End Function

Function ExternalIP()
url = "http://whatismyip.org"
set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "GET", url, false
xmlhttp.send ""
ExternalIP = xmlhttp.responseText
set xmlhttp = Nothing
End Function

Sub SendEmail(strTo, strSubject, strBody)
Set objEmail = CreateObject("CDO.Message")
objEmail.From = NotifyContact
objEmail.To = strTo
objEmail.Subject = strSubject
objEmail.HTMLBody = strBody
'objEmail.AddAttachment (txtOutFile)
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = smtpserver
objEmail.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Update
objEmail.Send
End Sub

This entry was posted in Scripting. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Notify me of followup comments via e-mail. You can also subscribe without commenting.