您的位置:首页 > 其它

导出系统日志并自动发送邮件到指定邮箱的一般方法(Windows server适用)

2012-04-11 12:02 1241 查看
笔者在工作中遇到这样的问题,如何每天将指定的系统日志发送到指定的邮箱,直接在邮件中查看系统日志,通过这样的方法,不需要登录每台服务器去查看系统日志,大致的做法如下:

1. 采用批处理搜集系统日志到一个指定文件

2. 将这个文件的内容作为邮件的正文发送到指定邮箱

3. 安排计划任务,设置指定的时间发送

以下对各个步骤进行详细的描述,希望对大家的工作有帮助

1. 收集系统日志到指定文件,采用的是eventquery.vbs这个工具,这是系统自带的工具,详细的使用方法见微软KB: http://technet.microsoft.com/zh-cn/library/cc772995(WS.10).aspx ,举例:导出最近3条event id 为1003的事件的记录:

echo ==========应用程序报警================ > c:\event.txt

eventquery.vbs /r 3 /L application /FI "id eq 1003" /FO list /v >> c:\event.txt

设置cscript为指定编译器:cscript //h:cscript //s

将以上代码拷贝为bat文件格式,执行完成之后打开event.txt,可以获得内容

2. 将获得的内容作为邮件的内容并发送,脚本如下

content= "c:\event.txt"

set fso=createobject("scripting.filesystemobject")

if fso.fileexists(content) then

set fil=fso.getfile(content)

filename=fil.name

if lcase(right(filename,4))=".txt" then

set txt=fso.opentextfile(content,1)

code=txt.readall

txt.close

end if

end if

nr=code

Const Email_From = "abc@163.com"

Const Password = "**********"

Const Email_To = "def@139.com"

Set CDO = CreateObject("CDO.Message") '创建CDO.Message对象

CDO.Subject = "Server test" '邮件主题

CDO.From = Email_From '发件人地址

CDO.To = Email_To '收件人地址

CDO.TextBody = nr '邮件正文

'cdo.AddAttachment = "C:\hello.txt" '邮件附件文件路径

Const schema = "http://schemas.microsoft.com/cdo/configuration/"

With CDO.Configuration.Fields '用with关键字减少代码输入

.Item(schema & "sendusing") = 2 '使用网络上的SMTP服务器而不是本地的SMTP服务器

.Item(schema & "smtpserver") = "smtp.163.com" 'SMTP服务器地址

.Item(schema & "smtpauthenticate") = 1 '服务器认证方式

.Item(schema & "sendusername") = Email_From '发件人邮箱

.Item(schema & "sendpassword") = Password '发件人邮箱密码

.Item(schema & "smtpserverport") = 25 'SMTP服务器端口

.Item(schema & "smtpusessl") = True '是否使用SSL

.Item(schema & "smtpconnectiontimeout") = 60 '连接服务器的超时时间

.Update '更新设置

End With

CDO.Send '发送邮件

msgbox "Email sent!"

把上面的代码保存为automail.vbs.

3. 将上面两个步骤合并在一起,使用BAT,内容如下:

:应用程序报警

echo ==========应用程序报警================ > c:\event.txt

eventquery.vbs /r 3 /L application /FI "id eq 1003" /FO list /v >> c:\event.txt

call automail.vbs

把这个代码保存为export.bat

4.使用系统的计划任务,安排计划,可以设定每天下午三点钟自动发送日志到指定邮箱

5.补充:本地的SMTP服务器邮件发送部分代码如下

With CDO.Configuration.Fields

.Item(schema & "sendusing") = 2

.Item(schema & "smtpserver") = "10.10.10.10" ‘内部smtp地址

.Item(schema & "smtpauthenticate") = 0

.Item(schema & "sendusername") = Email_From

'.Item(schema & "sendpassword") = Password

.Item(schema & "smtpserverport") = 25

.Item(schema & "smtpusessl") = False

.Item(schema & "smtpconnectiontimeout") = 60

.Update

End With
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐