主要功能
1.当对于一个IP、10次无法Ping通后发送一份邮件。(不重复发送)
2.当网络恢复后、ping通一次后发一份恢复邮件(不重复发送)
3.在Ping通状态的时候的Ping间隔时间为1秒
4.循环监视PingIP,直到手动取消停止脚本
- '*********************************************************************
- '* File: PingIP
- '* Created: OCT , 2011
- '* Last Modified: OCT 30, 2011
- '* Version: 1.0
- '*
- '* Main Function: Ping an IP and Mail the result
- '*
- '*
- '* Create by Mark.Xu
- '*
- '**********************************************************************
- Option Explicit
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'Declare variables
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- Dim intPingStatusCode
- Dim intPingCount
- Dim strIPaddress
- Dim strErr,strErrTop
- Dim blSendMail,blPingErr
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'Give Value to variables
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- strIPaddress = "192.168.1.1"
- intPingCount = 0
- blSendMail = False
- blPingErr = False
- strErrtop = "Ping " & strIPaddress & vbCr
- 'The Variables about Mail
- Dim strSMTP,strSender,strSendto,strTitle,strContext,strAttachment1
- Dim strSendmail
- Dim mailarray(5,0) 'The mail information to array
- strSMTP = "192.168.10.1"
- strSender = "abc@abc.com"
- strSendto = "abc@abc.com"
- strTitle = ""
- strContext = ""
- strAttachment1 = ""
- Do Until intPingStatusCode = 100
- intPingStatusCode = PingCheck(strIPaddress)
- If intPingStatusCode <> 0 Then
- intPingCount = intPingCount + 1
- If blSendMail = false then
- strErr = strErr & intPingCount & ": " & PingErrDescript(intPingStatusCode) & vbCr
- End if
- If intPingCount = 10 Then
- blPingErr = True
- intPingCount = 0
- End If
- Else
- intPingCount = 0
- blPingErr = False
- WScript.Sleep 1000
- End If
- If blPingErr = true Then
- If blSendMail = False Then
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'To Send Mail
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- strTitle = "Ping " & strIPaddress
- strContext = strErrtop & strErr
- mailarray(0,0) = strSMTP
- mailarray(1,0) = strSender
- mailarray(2,0) = strSendto
- mailarray(3,0) = strTitle
- mailarray(4,0) = strContext
- mailarray(5,0) = strAttachment1
- strSendmail = SendMail(mailarray)
- blSendMail = True
- strErr = ""
- End If
- Else
- If blSendMail = True Then
- strTitle = "PingRecover " & strIPaddress
- strContext = "PingRecover " & strIPaddress
- mailarray(0,0) = strSMTP
- mailarray(1,0) = strSender
- mailarray(2,0) = strSendto
- mailarray(3,0) = strTitle
- mailarray(4,0) = strContext
- mailarray(5,0) = strAttachment1
- strSendmail = SendMail(mailarray)
- blSendMail = false
- End If
- End if
- Loop
- '********************************************************************
- '*
- '* Function PingErrDescript(intPing)
- '* Purpose: Change the WMI Win32_PingStatus StatusCode
- '* Input: intStatusCode
- '*
- '* Output: strResult
- '* Notes:
- '********************************************************************
- Function PingErrDescript(intPing)
- Select Case intPing
- Case 0:
- PingErrDescript = "Success"
- Case 11001:
- PingErrDescript = "Buffer Too Small"
- Case 11002:
- PingErrDescript = "Destination Net Unreachable"
- Case 11003:
- PingErrDescript = "Destination Host Unreachable"
- Case 11004:
- PingErrDescript = "Destination Protocol Unreachable"
- Case 11005:
- PingErrDescript = "Destination Port Unreachable"
- Case 11006:
- PingErrDescript = "No Resources"
- Case 11007:
- PingErrDescript = "Bad Option"
- Case 11008:
- PingErrDescript = "Hardware Error"
- Case 11009:
- PingErrDescript = "Packet Too Big"
- Case 11010:
- PingErrDescript = "Request Timed Out"
- Case 11011:
- PingErrDescript = "Bad Request"
- Case 11012:
- PingErrDescript = "Bad Route"
- Case 11013:
- PingErrDescript = "TimeToLive Expired Transit"
- Case 11014:
- PingErrDescript = "TimeToLive Expired Reassembly"
- Case 11015:
- PingErrDescript = "Parameter Problem"
- Case 11016:
- PingErrDescript = "Source Quench"
- Case 11017:
- PingErrDescript = "Option Too Big"
- Case 11018:
- PingErrDescript = "Bad Destination"
- Case 11032:
- PingErrDescript = "Negotiating IPSEC"
- Case 11050:
- PingErrDescript = "General Failure"
- End Select
- End function
- '********************************************************************
- '*
- '* Function PingCheck(strServer)
- '* Purpose: Use WMI to Ping an IP
- '* Input: strIPaddress
- '*
- '* Output:intStatusCode
- '* Notes:
- '********************************************************************
- Function PingCheck(strServer)
- Dim objWMIService,objItem
- Dim colItems
- Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
- Set colItems = objWMIService.ExecQuery("Select * From Win32_PingStatus Where Address='" & strServer & "'")
- For Each objItem In colItems
- PingCheck = objItem.StatusCode
- Exit For
- Next
- End Function
- '********************************************************************
- '*
- '* Function SendMail(arrayMail2d)
- '* Purpose: To Send mail
- '* Input:
- '*
- '* Output:
- '* Notes:
- '********************************************************************
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'mailarray format
- '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- 'mailarray(0,0) is SMTP
- 'mailarray(1,0) is Sender
- 'mailarray(2,0) is Sendto
- 'mailarray(3,0) is Title
- 'mailarray(4,0) is Textbody
- 'mailarray(5,0) is Attachment,if nothing then be null
- 'Dim mailarray(5,1)
- 'mailarray(0,0) = "smtp.mailtest.local"
- 'mailarray(1,0) = "ServerAlert@mailtest.local"
- 'mailarray(2,0) = "mailadmin@mailtest.local"
- 'mailarray(3,0) = "Server ReStart"
- 'mailarray(4,0) = "OK ReStart"
- 'mailarray(5,1) = "D:\script_center.exe"
- '*
- '*
- '********************************************************************
- Function SendMail(arrayMail2d)
- On Error Resume Next
- Dim objMessage
- Dim y
- Set objMessage = CreateObject("CDO.Message")
- objMessage.Subject = arrayMail2d(3,0)
- objMessage.Sender = arrayMail2d(1,0)
- objMessage.To = arrayMail2d(2,0)
- objMessage.TextBody = arrayMail2d(4,0)
- For y = 0 To UBound(arraymail2d,2)
- If Trim(arraymail2d(5,y)) <> "" Then
- objMessage.AddAttachment arraymail2d(5,y)
- Else
- 'WScript.Echo arraymail2d(5,y)
- End If
- Next
- '==This section provides the configuration information for the remote SMTP server.
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
- 'Name or IP of Remote SMTP Server
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = arrayMail2d(0,0)
- 'Server port (typically 25)
- objMessage.Configuration.Fields.Item _
- ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
- objMessage.Configuration.Fields.Update
- objMessage.Send
- If Err.Number = 0 then
- SendMail = Err.Number
- Else
- SendMail = Err.Number & "," & Err.Description
- Err.Clear
- End If
- On Error Goto 0
- End Function
转载于:https://blog.51cto.com/381691/706072