vbs 中带有电子邮件的 Ping 脚本

发布于 2024-09-05 21:02:40 字数 1924 浏览 6 评论 0原文

我知道我已经问过有关 ping 脚本的问题,但现在我有一个新问题:-) 我希望有人能再次帮助我。

strText = "here comes the mail message"

strFile = "test.log"

PingForever strHost, strFile

Sub PingForever(strHost, outputfile)
    Dim Output, Shell, strCommand, ReturnCode

    Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile(outputfile, 8, True)
    Set Shell = CreateObject("wscript.shell")
    strCommand = "ping -n 1 -w 300 " & strHost
    While(True)
        ReturnCode = Shell.Run(strCommand, 0, True)     
        If ReturnCode = 0 Then
            Output.WriteLine Date() & " - " & Time & " | " & strHost & " - ONLINE"
        Else
            Output.WriteLine Date() & " - " & Time & " | " & strHost & " - OFFLINE"

            Set objEmail = CreateObject("CDO.Message")
            objEmail.From = "[email protected]"
            objEmail.To = "[email protected]"
            objEmail.Subject = "Computer" & strHost & " is offline" 
            objEmail.Textbody = strText
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                    "smtpadress" 
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objEmail.Configuration.Fields.Update
            objEmail.Send

        End If
        Wscript.Sleep 2000
    Wend
End Sub

我现在的问题是,当计算机处于离线状态时,邮件只发送了 2 秒。有人可以告诉我如何用旗帜制作它吗?那么离线时只会收到一封邮件吗?

感谢您的帮助。

i know i asked already the question about the ping script but now i have a new question about it :-) I hope someone can help me again.

strText = "here comes the mail message"

strFile = "test.log"

PingForever strHost, strFile

Sub PingForever(strHost, outputfile)
    Dim Output, Shell, strCommand, ReturnCode

    Set Output = CreateObject("Scripting.FileSystemObject").OpenTextFile(outputfile, 8, True)
    Set Shell = CreateObject("wscript.shell")
    strCommand = "ping -n 1 -w 300 " & strHost
    While(True)
        ReturnCode = Shell.Run(strCommand, 0, True)     
        If ReturnCode = 0 Then
            Output.WriteLine Date() & " - " & Time & " | " & strHost & " - ONLINE"
        Else
            Output.WriteLine Date() & " - " & Time & " | " & strHost & " - OFFLINE"

            Set objEmail = CreateObject("CDO.Message")
            objEmail.From = "[email protected]"
            objEmail.To = "[email protected]"
            objEmail.Subject = "Computer" & strHost & " is offline" 
            objEmail.Textbody = strText
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                    "smtpadress" 
            objEmail.Configuration.Fields.Item _
                ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
            objEmail.Configuration.Fields.Update
            objEmail.Send

        End If
        Wscript.Sleep 2000
    Wend
End Sub

My problem is now, the mail comes all 2 seconds, when the computer are offline. Can someone show me how to make it with flags? So only one mail comes when its offline?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

鸵鸟症 2024-09-12 21:02:40

仅在状态更改时使用标志并报告

FLAG0 = "ON"
While(True)
    ReturnCode = Shell.Run(strCommand, 0, True)     
    If ReturnCode = 0 Then
        Output.WriteLine Date() & " - " & Time & " | " & strHost & " - ONLINE"
        FLAG0 = "ON"
    Else
        Output.WriteLine Date() & " - " & Time & " | " & strHost & " - OFFLINE"
        IF FLAG0 = "ON" THEN
           FLAG0 = "OFF"
           Set objEmail = CreateObject("CDO.Message")
           ...... rest of mailing code
        END IF

    End If

Use a flag and report only when state changes

FLAG0 = "ON"
While(True)
    ReturnCode = Shell.Run(strCommand, 0, True)     
    If ReturnCode = 0 Then
        Output.WriteLine Date() & " - " & Time & " | " & strHost & " - ONLINE"
        FLAG0 = "ON"
    Else
        Output.WriteLine Date() & " - " & Time & " | " & strHost & " - OFFLINE"
        IF FLAG0 = "ON" THEN
           FLAG0 = "OFF"
           Set objEmail = CreateObject("CDO.Message")
           ...... rest of mailing code
        END IF

    End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文