Link to home
Start Free TrialLog in
Avatar of bsharath
bsharathFlag for India

asked on

DHCP Scripr error

Hi,

I have a DHCP vb script but gives an error can any one help me in this.

'**** DHCP Server Log Checking Script
'**** Copyright © 2005 Chris Pratt
'**** You may use this script free of charge but may not make any changes except to the variables to meet your needs
'**** without the permission of the author.
'****
'****
'**** The script is best set-up to run as a scheduled task. It can check for any event ID in the DHCP Server logs.
'**** It does not need to run on the DHCP server but you will need the full UNC path and the user that the script
'**** is set to run as will need rights to the servers log file areas.
'****
'**** The time interval is configurable and your scheduled task running time should be set to match that so areas of the
'**** logs don't go unchecked.

'Variables

strfindtime = dateadd("n", -30, Time)           ' Change this figure (-30) to set interval between checking (in minutes)
intfindtime = 30 ' Change this value to match the one above
strtoday = (WeekdayName(Weekday(Date),true))
strlogfile = "\\inexchange1\c$\WINDOWS\system32\dhcp\dhcpsrvlog." & strtoday    ' Change the locations to match that of your DHCP Server
streventid = "10"    ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's
strTo = "sharath.reddy@***.com" ' E-mail address TO
strSubject = "DHCP Server Alert" ' E-mail Subject
objCDOMail.From = "sharath.reddy@***.com" 'E-mail from address (needs to be a valid address)
objCDOMail.From    =  "SYSTEM <INEXCHANGE1.***.co.uk>" ' E-mail from address (needs to be a valid address)

'Leave the rest of the script

Dim strTo, strSubject, strBody, i
Dim objCDOMail
set objCDOMail = CreateObject("CDONTS.NewMail")
const forreading = 1
set objfso = createobject("Scripting.filesystemobject")
set objtextfile = objfso.opentextfile(strlogfile, forreading)
do while objtextfile.atendofstream <> True
ceventid = ""
posfind = 0
      
strline = objtextfile.readline

if instr(strline, "ID Date,Time,Description,IP Address,Host Name,MAC Address") then
            strline = objtextfile.readline
      else

      end if

if instr(strline, ",") then
            arrdhcprecord = split(strline, ",", 7)
            ceventid = arrdhcprecord(0)
            ceventdte = arrdhcprecord(1)
            ceventtime = arrdhcprecord(2)
            ceventdesc = arrdhcprecord(3)
            ceventip = arrdhcprecord(4)
            ceventhost = arrdhcprecord(5)
            ceventmac = arrdhcprecord(6)
      else

      End if
      i=i+1

findtime = datediff("n", ceventtime, strfindtime)

if findtime < intfindtime then
            posfind = posfind + 1
      else

      end if

if ceventid = streventid then
            posfind = posfind + 1
else

end if

if posfind = 2 then
            strBody = "===================" & vbCrLf
            strBody = strBody & " DHCP Server Alert" & vbCrLf
            strBody = strBody & "===================" & vbCrLf
            strBody = strBody & vbCrLf
            strBody = strBody & "Event ID: " & ceventid & vbCrLf
            strBody = strBody & "Date: " & ceventdte & vbCrLf
            strBody = strBody & "Time: " & ceventtime & vbCrLf
            strBody = strBody & "Desc: " & ceventdesc & vbCrLf
            strBody = strBody & "IP: " & ceventip & vbCrLf
            strBody = strBody & "Host: " & ceventhost & vbCrLf
            strBody = strBody & "Mac:" & ceventmac & vbCrLf
            objCDOMail.To      = strTo
            objCDOMail.Subject = strSubject
            objCDOMail.Body    = strBody
            objCDOMail.Send
      else

      end if

loop
objtextfile.close
Set objCDOMail = Nothing
Avatar of mailtosinghs
mailtosinghs

Please tell us what is the error you are getting while executing this script
Avatar of bsharath

ASKER

---------------------------
Windows Script Host
---------------------------
Script:      C:\Documents and Settings\sharathr\Desktop\dhcpchecker_287\dhcpchecker.vbs
Line:      23
Char:      1
Error:      Object required: ''
Code:      800A01A8
Source:       Microsoft VBScript runtime error

---------------------------
OK  
---------------------------
Create another variable on line 23 and 24;

Line 23, and 24 to after line 30 where you have written "set objCDOMail = CreateObject("CDONTS.NewMail")"

this will help

this error was because you have tried to use the object "objCDOMail" before initializing it.
you can create variable to define to and from email address.

use these variables in the form.

objCDOMail.From = variable1
objCDOMail.From    = variable2
change this portion of script



'Variables

estrfindtime = dateadd("n", -30, Time)           ' Change this figure (-30) to set interval between checking (in minutes)
intfindtime = 30 ' Change this value to match the one above
strtoday = (WeekdayName(Weekday(Date),true))
strlogfile = "\\inexchange1\c$\WINDOWS\system32\dhcp\dhcpsrvlog." & strtoday    ' Change the locations to match that of your DHCP Server
streventid = "10"    ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's
strTo = "sharath.reddy@***.com" ' E-mail address TO
strSubject = "DHCP Server Alert" ' E-mail Subject
objCDOMail.From = "sharath.reddy@***.com" 'E-mail from address (needs to be a valid address)
objCDOMail.From    =  "SYSTEM <INEXCHANGE1.***.co.uk>" ' E-mail from address (needs to be a valid address)

'Leave the rest of the script

Dim strTo, strSubject, strBody, i
Dim objCDOMail
set objCDOMail = CreateObject("CDONTS.NewMail")
const forreading = 1
set objfso = createobject("Scripting.filesystemobject")
set objtextfile = objfso.opentextfile(strlogfile, forreading)
do while objtextfile.atendofstream <> True
ceventid = ""
posfind = 0




to




'Variables

strfindtime = dateadd("n", -30, Time)           ' Change this figure (-30) to set interval between checking (in minutes)
intfindtime = 30 ' Change this value to match the one above
strtoday = (WeekdayName(Weekday(Date),true))
strlogfile = "\\inexchange1\c$\WINDOWS\system32\dhcp\dhcpsrvlog." & strtoday    ' Change the locations to match that of your DHCP Server
streventid = "10"    ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's
strTo = "sharath.reddy@***.com" ' E-mail address TO
strSubject = "DHCP Server Alert" ' E-mail Subject



'Leave the rest of the script

Dim strTo, strSubject, strBody, i
Dim objCDOMail
set objCDOMail = CreateObject("CDONTS.NewMail")
objCDOMail.From = strTo 'you have already defined the email-to field
objCDOMail.From    =  "SYSTEM <INEXCHANGE1.***.co.uk>" ' E-mail from address (neds to be a valid address)
const forreading = 1
set objfso = createobject("Scripting.filesystemobject")
set objtextfile = objfso.opentextfile(strlogfile, forreading)
do while objtextfile.atendofstream <> True
ceventid = ""
posfind = 0



this should work :)
mailtosinghs:
Now i get this error

C:\>cscript a.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

C:\a.vbs(88, 3) (null): 0x80040108
this error is related to CDONTS

can you tell me what is on 88th line in that file...



This is from the first line

strBody = strBody & "===================" & vbCrLf


Or started count from 'Variables

if posfind = 2 then
            strBody = "===================" & vbCrLf
            strBody = strBody & " DHCP Server Alert" & vbCrLf
            strBody = strBody & "===================" & vbCrLf
            strBody = strBody & vbCrLf
            strBody = strBody & "Event ID: " & ceventid & vbCrLf
            strBody = strBody & "Date: " & ceventdte & vbCrLf
            strBody = strBody & "Time: " & ceventtime & vbCrLf
            strBody = strBody & "Desc: " & ceventdesc & vbCrLf
            strBody = strBody & "IP: " & ceventip & vbCrLf
            strBody = strBody & "Host: " & ceventhost & vbCrLf
            strBody = strBody & "Mac:" & ceventmac & vbCrLf
            objCDOMail.To      = strTo
            objCDOMail.Subject = strSubject
            objCDOMail.Body    = strBody
            objCDOMail.Send
      else


Else is on the 80th line
please mail me this in .txt attachment to my user name here  at yahoo dot com

I dont know whether posting email address for attachments are allowed or not;
alternatively you can paste the entire script here again
Here is the full script


'**** DHCP Server Log Checking Script
'**** Copyright © 2005 Chris Pratt
'**** You may use this script free of charge but may not make any changes except to the variables to meet your needs
'**** without the permission of the author.
'****
'****
'**** The script is best set-up to run as a scheduled task. It can check for any event ID in the DHCP Server logs.
'**** It does not need to run on the DHCP server but you will need the full UNC path and the user that the script
'**** is set to run as will need rights to the servers log file areas.
'****
'**** The time interval is configurable and your scheduled task running time should be set to match that so areas of the
'**** logs don't go unchecked.
'Variables

strfindtime = dateadd("n", -30, Time)           ' Change this figure (-30) to set interval between checking (in minutes)
intfindtime = 30 ' Change this value to match the one above
strtoday = (WeekdayName(Weekday(Date),true))
strlogfile = "\\in-ads-srv02\c$\WINNT\system32\dhcp\DhcpSrvLog." & strtoday    ' Change the locations to match that of your DHCP Server
streventid = "10"    ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's
strTo = "sharath.reddy@" ' E-mail address TO
strSubject = "DHCP Server Alert" ' E-mail Subject



'Leave the rest of the script

Dim strTo, strSubject, strBody, i
Dim objCDOMail
set objCDOMail = CreateObject("CDONTS.NewMail")
objCDOMail.From = strTo 'you have already defined the email-to field
objCDOMail.From    =  "SYSTEM <sharath.reddy@com>" ' E-mail from address (neds to be a valid address)
const forreading = 1
set objfso = createobject("Scripting.filesystemobject")
set objtextfile = objfso.opentextfile(strlogfile, forreading)
do while objtextfile.atendofstream <> True
ceventid = ""
posfind = 0

      
strline = objtextfile.readline

if instr(strline, "ID Date,Time,Description,IP Address,Host Name,MAC Address") then
            strline = objtextfile.readline
      else

      end if

if instr(strline, ",") then
            arrdhcprecord = split(strline, ",", 7)
            ceventid = arrdhcprecord(0)
            ceventdte = arrdhcprecord(1)
            ceventtime = arrdhcprecord(2)
            ceventdesc = arrdhcprecord(3)
            ceventip = arrdhcprecord(4)
            ceventhost = arrdhcprecord(5)
            ceventmac = arrdhcprecord(6)
      else

      End if
      i=i+1

findtime = datediff("n", ceventtime, strfindtime)

if findtime < intfindtime then
            posfind = posfind + 1
      else

      end if

if ceventid = streventid then
            posfind = posfind + 1
else

end if

if posfind = 2 then
            strBody = "===================" & vbCrLf
            strBody = strBody & " DHCP Server Alert" & vbCrLf
            strBody = strBody & "===================" & vbCrLf
            strBody = strBody & vbCrLf
            strBody = strBody & "Event ID: " & ceventid & vbCrLf
            strBody = strBody & "Date: " & ceventdte & vbCrLf
            strBody = strBody & "Time: " & ceventtime & vbCrLf
            strBody = strBody & "Desc: " & ceventdesc & vbCrLf
            strBody = strBody & "IP: " & ceventip & vbCrLf
            strBody = strBody & "Host: " & ceventhost & vbCrLf
            strBody = strBody & "Mac:" & ceventmac & vbCrLf
            objCDOMail.To      = strTo
            objCDOMail.Subject = strSubject
            objCDOMail.Body    = strBody
            objCDOMail.Send
      else

      end if

loop
objtextfile.close
Set objCDOMail = Nothing
Use the modified script. I have moved set objcdomail.... to inside the while loop



'**** DHCP Server Log Checking Script
'**** Copyright © 2005 Chris Pratt
'**** You may use this script free of charge but may not make any changes except to the variables to meet your needs
'**** without the permission of the author.
'****
'****
'**** The script is best set-up to run as a scheduled task. It can check for any event ID in the DHCP Server logs.
'**** It does not need to run on the DHCP server but you will need the full UNC path and the user that the script
'**** is set to run as will need rights to the servers log file areas.
'****
'**** The time interval is configurable and your scheduled task running time should be set to match that so areas of the
'**** logs don't go unchecked.
'Variables

strfindtime = dateadd("n", -30, Time)           ' Change this figure (-30) to set interval between checking (in minutes)
intfindtime = 30 ' Change this value to match the one above
strtoday = (WeekdayName(Weekday(Date),true))
strlogfile = "\\in-ads-srv02\c$\WINNT\system32\dhcp\DhcpSrvLog." & strtoday    ' Change the locations to match that of your DHCP Server
streventid = "10"    ' Change to meet what event you want to check for - See the beginning of a log to see a definition list of the Event ID's
strTo = "sharath.reddy@isc.com" ' E-mail address TO
strSubject = "DHCP Server Alert" ' E-mail Subject



'Leave the rest of the script

Dim strTo, strSubject, strBody, i
Dim objCDOMail
const forreading = 1
set objfso = createobject("Scripting.filesystemobject")
set objtextfile = objfso.opentextfile(strlogfile, forreading)
do while objtextfile.atendofstream <> True
ceventid = ""
posfind = 0

     
strline = objtextfile.readline

if instr(strline, "ID Date,Time,Description,IP Address,Host Name,MAC Address") then
            strline = objtextfile.readline
      else

      end if

if instr(strline, ",") then
            arrdhcprecord = split(strline, ",", 7)
            ceventid = arrdhcprecord(0)
            ceventdte = arrdhcprecord(1)
            ceventtime = arrdhcprecord(2)
            ceventdesc = arrdhcprecord(3)
            ceventip = arrdhcprecord(4)
            ceventhost = arrdhcprecord(5)
            ceventmac = arrdhcprecord(6)
      else

      End if
      i=i+1

findtime = datediff("n", ceventtime, strfindtime)

if findtime < intfindtime then
            posfind = posfind + 1
      else

      end if

if ceventid = streventid then
            posfind = posfind + 1
else

end if

if posfind = 2 then
            strBody = "===================" & vbCrLf
            strBody = strBody & " DHCP Server Alert" & vbCrLf
            strBody = strBody & "===================" & vbCrLf
            strBody = strBody & vbCrLf
            strBody = strBody & "Event ID: " & ceventid & vbCrLf
            strBody = strBody & "Date: " & ceventdte & vbCrLf
            strBody = strBody & "Time: " & ceventtime & vbCrLf
            strBody = strBody & "Desc: " & ceventdesc & vbCrLf
            strBody = strBody & "IP: " & ceventip & vbCrLf
            strBody = strBody & "Host: " & ceventhost & vbCrLf
            strBody = strBody & "Mac:" & ceventmac & vbCrLf
            set objCDOMail = CreateObject("CDONTS.NewMail")
            objCDOMail.From    =  "SYSTEM <sharath.reddy@i.com>" ' E-mail from address (neds to be a valid address)
            objCDOMail.To      = strTo
            objCDOMail.Subject = strSubject
            objCDOMail.Body    = strBody
            objCDOMail.Send
            Set objCDOMail = Nothing
      else

      end if

loop
objtextfile.close
Hi

Some thing went on it came out without any error but how do i find it is working.
Until you have understood can you tell me what this script does
THX
Sharath
if you wnat to know that it went into the loop or not you can insert the line

WScript.Echo "some text"

and run the script again, you should see the echo message while the script in loop.

if this was not what you need, please specify the qn again clearly
please insert above line before the

objCDOMail.Send
I got a box containing "Sometext"

But where will i get the result.I now that this is correct but where can i see the result.

THX
For replying so patiently.

THX
Sharath
You will get the result in the email address mentioned in the script which is sharath.reddy@isc.com here.

For a successful email, you need direct internet connectivity of the system where this script is running, or you have to setup intranet properly
This mail will come when ever a error is encountered on the server or ?? Is there any wany i can genetrate a faulse error or through the script can we send a test mail.
this will send a mail if you have eventid 10 in today's dhcp log in past 30 min.

if you want to generate an email do this:.....

open
\\in-ads-srv02\c$\WINNT\system32\dhcp\DhcpSrvLog." & strtoday
for this go to the folder "\\in-ads-srv02\c$\WINNT\system32\dhcp\"
open today's logfile dhcpsrvlog.mon if day is monday
you will find following texts after scrolling some lines


ID Date,Time,Description,IP Address,Host Name,MAC Address
00,04/19/99,12:43:06,Started,,,
60,04/19/99,12:43:21,No DC is DS Enabled,,MYDOMAIN,
63,04/19/99,12:43:28,Restarting rogue detection,,,
01,04/19/99,13:11:13,Stopped,,,
00,04/19/99,12:43:06,Started,,,
55,04/19/99,12:43:54,Authorized(servicing),,MYDOMAIN,

change 10 in following line of code to 00 in the script file.
streventid = "10"


stop and restart the dhcp service.
rerun the script


please remove the line
WScript.Echo "some text"
or change this to a comment
the log text i have posted is a sample, you may find different texts but in same format
In the log file what should i change
I have changed all 10 to 00 the restart the DHCP service the ran the script but did not receive any mails

THX
Sharath
ASKER CERTIFIED SOLUTION
Avatar of mailtosinghs
mailtosinghs

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi,

I have no clue on what you said now sorry to say i dont know anything about scripting.Please help me in this regard.

THX
Sharath
Thanks for the support.Even though i havent got the solution i am closing the question