今天为公司服务器添加脚本监测硬盘空间后,发邮件警报,一直无法成功发送邮件,后来才发现使用的jmail需要通过内部的一个邮件服务器发送,而该服务器又做了IP限制,所幸到自己弄一个。
'************** liq ************
Const cdoSendUsingMethod="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort=2
Const cdoSMTPServer="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate="http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic=0 ' 1 需要验证 0 不需要验证
Const cdoSendUserName="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "mx.hichina.com" '改成可用的外部邮件服务器域名
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic ' cdoBasic = 0 时 不需要验证服务器用户名密码
'.Item(cdoSendUserName) = "" '以上服务器的用户名
'.Item(cdoSendPassword) = "" '密码
.Update
End With
Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.To = "liq@hichina.com;gaojl@hichina.com;liangjy@hichina.com;jiangxh@hichina.com" '改成接收者的邮件地址
.From = "API@hichina.com" '改成发送人的邮件地址 貌似可以随便填
.Subject =subject '标题
.TextBody =body '正文
'.AddAttachment "C:\Scripts\Output.txt"'邮件附件
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
'**********************
Const cdoSendUsingMethod="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort=2
Const cdoSMTPServer="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate="http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic=0 ' 1 需要验证 0 不需要验证
Const cdoSendUserName="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "mx.hichina.com" '改成可用的外部邮件服务器域名
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic ' cdoBasic = 0 时 不需要验证服务器用户名密码
'.Item(cdoSendUserName) = "" '以上服务器的用户名
'.Item(cdoSendPassword) = "" '密码
.Update
End With
Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.To = "liq@hichina.com;gaojl@hichina.com;liangjy@hichina.com;jiangxh@hichina.com" '改成接收者的邮件地址
.From = "API@hichina.com" '改成发送人的邮件地址 貌似可以随便填
.Subject =subject '标题
.TextBody =body '正文
'.AddAttachment "C:\Scripts\Output.txt"'邮件附件
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
'**********************
另附上全部包括硬盘空间监控脚本:
Send_Mail = 0
disk_limt = 80
file_name = "disk_info.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(file_name) Then
objFSO.DeleteFile(file_name)
End If
Set DiskSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_LogicalDisk where DriveType=3")
For each Disk in DiskSet
disk_info = Disk.Name & " " & FormatPercent(((Disk.Size-Disk.FreeSpace)/Disk.Size), 0) & " (" & _
FormatNumber((Disk.Size-Disk.FreeSpace)/1024/1024/1024) & "/" & FormatNumber(Disk.Size/1024/1024/1024) & "GB)"
'WScript.Echo disk_info
Call Write_Info(disk_info, file_name)
Next
Set ri = objFSO.OpenTextFile(file_name, 1, True)
Do While ri.AtEndOfStream <> True
my_string = Split(ri.ReadLine, " ", -1, 1)
used_percent = Split(my_string(1), "%", -1, 1)
'WScript.Echo used_percent(0)
If CInt(used_percent(0)) >= disk_limt Then
Send_Mail = 1
End If
Loop
ri.close
If Send_Mail = 1 Then
'WScript.Echo Send_Mail
' Send Mail
subject = Now & ", " & Get_Hostname & ", disk alert"
body = "API--" &Read_Body(file_name)
'WScript.Echo subject
'WScript.Echo body
'************** liq ************
Const cdoSendUsingMethod="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort=2
Const cdoSMTPServer="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate="http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic=0 ' 1 需要验证 0 不需要验证
Const cdoSendUserName="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "mx.hichina.com" '改成可用的外部邮件服务器域名
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic ' cdoBasic = 0 时 不需要验证服务器用户名密码
'.Item(cdoSendUserName) = "" '以上服务器的用户名
'.Item(cdoSendPassword) = "" '密码
.Update
End With
Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.To = "liq@hichina.com;gaojl@hichina.com;liangjy@hichina.com;jiangxh@hichina.com" '改成接收者的邮件地址
.From = "API@hichina.com" '改成发送人的邮件地址 貌似可以随便填
.Subject =subject '标题
.TextBody =body '正文
'.AddAttachment "C:\Scripts\Output.txt"'邮件附件
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
'**********************
End If
Set objFSO = nothing
Sub Write_Info(line, filename)
Set wi = objFSO.OpenTextFile(filename, 8, True)
wi.WriteLine line
wi.close
End Sub
Function Get_Hostname
Set WshShell = WScript.CreateObject("WScript.Shell")
Get_Hostname = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set WshShell = nothing
End Function
Function Read_Body(contentFile)
contentTemp = ""
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFL = objFS.OpenTextFile(contentFile, 1, True)
Do While objFL.AtEndOfStream <> True
contentTemp = contentTemp & objFL.ReadLine & vbCrLf
Loop
objFL.Close
Set objFS = Nothing
Read_Body = contentTemp
End Function
disk_limt = 80
file_name = "disk_info.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists(file_name) Then
objFSO.DeleteFile(file_name)
End If
Set DiskSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
("select * from Win32_LogicalDisk where DriveType=3")
For each Disk in DiskSet
disk_info = Disk.Name & " " & FormatPercent(((Disk.Size-Disk.FreeSpace)/Disk.Size), 0) & " (" & _
FormatNumber((Disk.Size-Disk.FreeSpace)/1024/1024/1024) & "/" & FormatNumber(Disk.Size/1024/1024/1024) & "GB)"
'WScript.Echo disk_info
Call Write_Info(disk_info, file_name)
Next
Set ri = objFSO.OpenTextFile(file_name, 1, True)
Do While ri.AtEndOfStream <> True
my_string = Split(ri.ReadLine, " ", -1, 1)
used_percent = Split(my_string(1), "%", -1, 1)
'WScript.Echo used_percent(0)
If CInt(used_percent(0)) >= disk_limt Then
Send_Mail = 1
End If
Loop
ri.close
If Send_Mail = 1 Then
'WScript.Echo Send_Mail
' Send Mail
subject = Now & ", " & Get_Hostname & ", disk alert"
body = "API--" &Read_Body(file_name)
'WScript.Echo subject
'WScript.Echo body
'************** liq ************
Const cdoSendUsingMethod="http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort=2
Const cdoSMTPServer="http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort="http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout="http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate="http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic=0 ' 1 需要验证 0 不需要验证
Const cdoSendUserName="http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword="http://schemas.microsoft.com/cdo/configuration/sendpassword"
Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "mx.hichina.com" '改成可用的外部邮件服务器域名
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSMTPAuthenticate) = cdoBasic ' cdoBasic = 0 时 不需要验证服务器用户名密码
'.Item(cdoSendUserName) = "" '以上服务器的用户名
'.Item(cdoSendPassword) = "" '密码
.Update
End With
Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig
With objMessage
.To = "liq@hichina.com;gaojl@hichina.com;liangjy@hichina.com;jiangxh@hichina.com" '改成接收者的邮件地址
.From = "API@hichina.com" '改成发送人的邮件地址 貌似可以随便填
.Subject =subject '标题
.TextBody =body '正文
'.AddAttachment "C:\Scripts\Output.txt"'邮件附件
.Send
End With
Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing
'**********************
End If
Set objFSO = nothing
Sub Write_Info(line, filename)
Set wi = objFSO.OpenTextFile(filename, 8, True)
wi.WriteLine line
wi.close
End Sub
Function Get_Hostname
Set WshShell = WScript.CreateObject("WScript.Shell")
Get_Hostname = WshShell.ExpandEnvironmentStrings("%COMPUTERNAME%")
Set WshShell = nothing
End Function
Function Read_Body(contentFile)
contentTemp = ""
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objFL = objFS.OpenTextFile(contentFile, 1, True)
Do While objFL.AtEndOfStream <> True
contentTemp = contentTemp & objFL.ReadLine & vbCrLf
Loop
objFL.Close
Set objFS = Nothing
Read_Body = contentTemp
End Function
浙公网安备 33010602011771号