Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write IP to Text and if Changed EMAIL

Status
Not open for further replies.

tqeonline

MIS
Oct 5, 2009
304
US
Hey guys,

I have a script that will send me an email. However I would like it to only send me an email if the ip changed from the previous day.

For instance I have it scheduled to run daily at 7am. It then compares the current IP to the previous IP (in a text file) and if it is different then email the new IP.

This is what i have
Code:
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objitem In colitems
   strIPAddress = Join(objitem.IPAddress, ",")
   'WScript.Echo strIPAddress
	
Set objEmail = CreateObject("CDO.Message")
	objEmail.From = "IPAddress@****.com"
	objEmail.To = "Matt.Loflin@****.com"
	objEmail.Subject = "IP Address - " & strComputerName
	objEmail.Textbody = strIPAddress
	objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
	objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = _
        "webmail.****.com" 
	objEmail.Configuration.Fields.Item _
    ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
	objEmail.Configuration.Fields.Update
	objEmail.Send
   Exit For
Next


- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
Try something like this:
Code:
[COLOR=blue]Const ForReading = 1, ForWriting = 2
Dim fso, f, sFileLastIPAddress, sLastIP
Set fso = CreateObject("Scripting.FileSystemObject")
sFileLastIPAddress = "c:\mypath\lastipaddress.txt"[/color]

Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objitem In colitems
   strIPAddress = Join(objitem.IPAddress, ",")
   'WScript.Echo strIPAddress

   [COLOR=blue]If fso.FileExists(sFileLastIPAddress) Then
      Set f = fso.OpenTextFile(sFileLastIPAddress, ForReading, True)
      sLastIP = f.ReadAll
   Else
      sLastIP = ""
   End If

   If sLastIP <> strIPAddress Then[/color]
      Set objEmail = CreateObject("CDO.Message")
      objEmail.From = "IPAddress@****.com"
      objEmail.To = "Matt.Loflin@****.com"
      objEmail.Subject = "IP Address - " & strComputerName
      objEmail.Textbody = strIPAddress
      objEmail.Configuration.Fields.Item _
         ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
      objEmail.Configuration.Fields.Item _
         ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = _
         "webmail.****.com"
      objEmail.Configuration.Fields.Item _
         ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
      objEmail.Configuration.Fields.Update
      objEmail.Send
      [COLOR=blue]Set f = fso.CreateTextFile(sFileLastIPAddress, True)
      f.Write strIPAddress[/color]
      Exit For
   [COLOR=blue]End If[/color]
Next
 
I just finished actually... my result was similar to yours.

Code:
'***GET CURRENT IP ADDRESS***
Set wshShell = WScript.CreateObject( "WScript.Shell" )
strComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputerName & "\root\cimv2")

Set colItems = objWMIService.ExecQuery ("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objitem In colitems
	strNEWIPAddress = Join(objitem.IPAddress, ",")
	Exit For
Next


'***GET OLD IP ADDRESS***
Set objFSO = CreateObject("Scripting.FileSystemObject")
FileName = "C:\IPAddress.txt"
IF objFSO.FileExists (FileName) then
	Set objFile = objFSO.GetFile(FileName)
	If objFile.Size > 0 Then
		Set objReadFile = objFSO.OpenTextFile(FileName, 1)
		strOLDIPAddress= objReadFile.ReadLine
		objReadFile.Close
	End If
ELSE
	strOLDIPAddress = ""
END IF

'***IF IP ADDRESS IS THE SAME DO NOTHING, ELSE EMAIL NEW IP AND SAVE NEW IP***
IF TRIM(strNEWIPAddress) <> TRIM(strOLDIPAddress) Then	
	Set objEmail = CreateObject("CDO.Message")
	objEmail.From = "IPAddress@Asurion.com"
	objEmail.To = "Matt.Loflin@Asurion.com"
	objEmail.Subject = "IP Address - " & strComputerName
	objEmail.Textbody = "NEW: " & strNEWIPAddress & vbcrlf & "OLD: " &  strOLDIPAddress
	objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/sendusing")[/URL] = 2
	objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserver")[/URL] = "webmail.asurion.com" 
	objEmail.Configuration.Fields.Item ("[URL unfurl="true"]http://schemas.microsoft.com/cdo/configuration/smtpserverport")[/URL] = 25
	objEmail.Configuration.Fields.Update
	objEmail.Send	

	DIM filesys, filetxt, getname, path
	SET filesys = CreateObject("Scripting.FileSystemObject")
	SET filetxt = filesys.CreateTextFile(FileName, True)
	PATH = filesys.GetAbsolutePathName(FileName)
	GETNAME = filesys.GetFileName(path)
	filetxt.WriteLine(strNEWIPAddress)
	filetxt.Close
END IF

- Matt

"If I must boast, I will boast of the things that show my weakness"

- Windows 2003 Server, 98 SE, XP
- VB.NET, VSTS 2010, ASP.NET, EXCEL VBA, ACCESS, SQL 2008
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top