服务器列表&将状态存储在文本文件中发送ping失败服务器的电子邮件

时间:2016-01-19 11:38:43

标签: vbscript

我正在尝试创建一个VBScript来ping服务器并在文本文件中获取其输出,并仅为ping失败的服务器发送邮件。

当下一次运行脚本时,它会检查文本文件中ping的状态并更新同一位置的当前状态,并且只应发送邮件进行状态更改。

我在下面编写了脚本,但它只在每次发现服务器状态时生成电子邮件。

Dim strInputPath, strOutputPath, strStatus
Dim objFSO, objTextIn, objTextOut

strInputPath = "E:\test\deep\INPUT.txt" 
strOutputPath = "E:\test\deep\OUTPUT.txt"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextIn = objFSO.OpenTextFile( strInputPath,1 )
Set objTextOut = objFSO.CreateTextFile( strOutputPath )
objTextOut.WriteLine("computer,status")

Do Until objTextIn.AtEndOfStream = True
    strComputer = objTextIn.ReadLine
    If fPingTest( strComputer ) Then
        strStatus = "UP"
    Else
        strStatus = "DOWN"
        Notify_Admins strComputer
    End If
    objTextOut.WriteLine(strComputer & "," & strStatus)
Loop

Function fPingTest( strComputer )
    Dim objShell,objPing
    Dim strPingOut, flag
    Set objShell = CreateObject("Wscript.Shell")
    Set objPing = objShell.Exec("ping " & strComputer)
    strPingOut = objPing.StdOut.ReadAll
    If InStr(LCase(strPingOut), "reply") Then
        flag = True
    Else
        flag = False
    End If
    fPingTest = flag
End Function

Sub Notify_Admins( strComputer )
    Set objEmail = CreateObject("CDO.Message")
    objEmail.From = "pradeep_kumar@gmail.com"
    objEmail.To = "kumarprade@ibm.com"
    objEmail.cc = "kumarprade@ibm.com"
    objEmail.Subject = strComputer & ": is Down"
    objEmail.Textbody = strComputer & " is not pingable on " & Now

    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
            "mail.dc.ibm.com" 
    objEmail.Configuration.Fields.Item _
        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
    objEmail.Configuration.Fields.Update

    objEmail.Send
End Sub

1 个答案:

答案 0 :(得分:0)

您需要输入文件中上一次运行的状态,否则您没有要比较的旧状态。使用输入文件中的(旧)状态,您可以将循环更改为以下内容:

Do Until objTextIn.AtEndOfStream
    line = Split(objTextIn.ReadLine, ",")
    computer = line(0)
    oldStatus = line(1)
    If fPingTest( computer ) Then
        status = "UP"
    Else
        status = "DOWN"
    End If

    If status <> oldStatus Then
        Notify_Admins computer
    End If

    objTextOut.WriteLine(computer & "," & status)
Loop

将输入文件替换为脚本末尾的输出文件:

objFSO.DeleteFile strInputPath
objFSO.MoveFile strOutputPath, strInputPath