{"id":18,"date":"2009-10-24T15:28:27","date_gmt":"2009-10-24T19:28:27","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=18"},"modified":"2009-10-24T15:33:20","modified_gmt":"2009-10-24T19:33:20","slug":"verify-network-translation","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/verify-network-translation\/","title":{"rendered":"Verify network translation"},"content":{"rendered":"<p>Again, some earlier code. \u00a0I&#8217;m not sure how good this works&#8230;I had ran it for a few weeks awhile back. \u00a0However, my IP address changed and I received a lot of notifications before I had a chance to resolve it. \u00a0The script would be better if it contained some sort of logic so that it only sent one notification.<\/p>\n<p>Monday, 08 October 2007<\/p>\n<ul>\n<li>The purpose of this script is to:<\/li>\n<li>lookup a domain name with nslookup, returning an IP address<\/li>\n<li>find the external network address translation (NAT) using whatismyip.org<\/li>\n<li>compare the two IP addresses<\/li>\n<li>send a notification if they are not the same<\/li>\n<\/ul>\n<p>This script was designed to run as a scheduled task and in my scenario it runs every 30 minutes.\u00a0 The purpose is to work similiar to the\u00a0<a style=\"font-size: 11px; color: blue; text-decoration: none; font-family: Tahoma, Verdana, Arial, Helvetica, sans-serif;\" href=\"http:\/\/www.no-ip.com\/\">www.no-ip.com<\/a> service that will dynamically update DNS based on your external IP address.\u00a0 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.<\/p>\n<p><code><br \/>\nNatName = \"publicnat.domainname.com\"<br \/>\nNotifyContact = \" scripting.user@domainname.com\"<br \/>\nsmtpserver = \"smtp.domainname.com\"<\/code><\/p>\n<p><code> <\/code><\/p>\n<p><code>IP_in_DNS = DNSLookup(NatName)<br \/>\nIP_on_OutSide = ExternalIP()<\/code><\/p>\n<p><code>If Not IP_in_DNS = IP_on_OutSide Then<br \/>\nSendEmail NotifyContact,NatName &amp; \" issue\",\"The network address translation for \" &amp; NatName &amp; \" (IP_\" &amp; IP_in_DNS &amp; \") does not match the current external address of IP_\" &amp; IP_on_OutSide &amp; \".\"<br \/>\nEnd If<\/code><\/p>\n<p><code>msgbox \"Done.\"<\/code><\/p>\n<p><code>Function DNSLookup(sDNSName)<br \/>\nOn error resume next<\/code><\/p>\n<p><code>Const OpenAsDefault = -2<br \/>\nConst FailIfNotExist = 0<br \/>\nConst ForReading = 1<\/code><\/p>\n<p><code>Set oShell = CreateObject(\"WScript.Shell\")<br \/>\nSet oFSO = CreateObject(\"Scripting.FileSystemObject\")<br \/>\nsTemp = oShell.ExpandEnvironmentStrings(\"%TEMP%\")<br \/>\nsTempFile = sTemp &amp; \"\\\" &amp; oFSO.GetTempName<\/p>\n<p>oShell.Run \"%comspec% \/c nslookup \" &amp; sDNSName &amp; \"&gt;\" &amp; sTempFile, 0, True<\/p>\n<p>Set fFile = oFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsDefault)<br \/>\nsResults = fFile.ReadAll<br \/>\nfFile.Close<br \/>\noFSO.DeleteFile (sTempFile)<\/p>\n<p>If InStr(sResults, \"Address:\") Then<br \/>\naNameTemp = Split(sResults, \"Address:\")<br \/>\naName = Split(Trim(aNameTemp(2)), Chr(13))<br \/>\n'aNameTemp(2) will be the second address listed; the first address is that of the DNS Server<\/p>\n<p>DNSLookup = aName(0)<br \/>\nElse<br \/>\nDNSLookup = \"Failed.\"<br \/>\nEnd If<\/p>\n<p>Set oShell = Nothing<br \/>\nSet oFSO = Nothing<br \/>\nEnd Function<\/p>\n<p>Function ExternalIP()<br \/>\nurl = \"http:\/\/whatismyip.org\"<br \/>\nset xmlhttp = CreateObject(\"MSXML2.ServerXMLHTTP\")<br \/>\nxmlhttp.open \"GET\", url, false<br \/>\nxmlhttp.send \"\"<br \/>\nExternalIP = xmlhttp.responseText<br \/>\nset xmlhttp = Nothing<br \/>\nEnd Function<\/p>\n<p><\/code><\/p>\n<p><code>Sub SendEmail(strTo, strSubject, strBody)<br \/>\nSet objEmail = CreateObject(\"CDO.Message\")<br \/>\nobjEmail.From = NotifyContact<br \/>\nobjEmail.To = strTo<br \/>\nobjEmail.Subject = strSubject<br \/>\nobjEmail.HTMLBody = strBody<br \/>\n'objEmail.AddAttachment (txtOutFile)<br \/>\nobjEmail.Configuration.Fields.Item _<br \/>\n(\"http:\/\/schemas.microsoft.com\/cdo\/configuration\/sendusing\") = 2<br \/>\nobjEmail.Configuration.Fields.Item _<br \/>\n(\"http:\/\/schemas.microsoft.com\/cdo\/configuration\/smtpserver\") = smtpserver<br \/>\nobjEmail.Configuration.Fields.Item _<br \/>\n(\"http:\/\/schemas.microsoft.com\/cdo\/configuration\/smtpserverport\") = 25<br \/>\nobjEmail.Configuration.Fields.Update<br \/>\nobjEmail.Send<br \/>\nEnd Sub<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Again, some earlier code. \u00a0I&#8217;m not sure how good this works&#8230;I had ran it for a few weeks awhile back. \u00a0However, my IP address changed and I received a lot of notifications before I had a chance to resolve it. &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/verify-network-translation\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":5,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"predecessor-version":[{"id":20,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions\/20"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}