Advertisement
duck__boy1981

VBS - Ping Servers

Apr 3rd, 2013
428
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Option Explicit
  2.    
  3. Dim objExplorer         ' Object used for displaying a 'Please wait...' box
  4. Dim strOutputSuccess    ' The results to output to the user for sussessful pings
  5. Dim strOutputFailure    ' The results to output to the user for any ping failures
  6.  
  7. ' Display the progress box
  8. Set objExplorer = CreateObject("InternetExplorer.Application")
  9.  
  10. display_progress(objExplorer)
  11. main()
  12. close_progress(objExplorer)
  13. output_results()
  14.  
  15. WScript.Quit
  16.  
  17. '**
  18. ' Runs the main bulk of the script
  19. '*
  20. Sub main()
  21.  
  22.     Dim objProgressDialog   ' Object to hold a progress dialog
  23.     Dim objWSShell          ' Object to hold a Windows Script Shell
  24.     Dim objWSExec           ' Object to hold the results of an Exec call
  25.     Dim strTarget(3,1)      ' The IP addresses to ping
  26.     Dim strPingResultsFull  ' The full results of the ping
  27.     Dim strPingResultsPart  ' The line of the results to output to the user
  28.     Dim strSingleLine       ' A single line of the output (used while looping to grab only the required lines)
  29.     Dim i                   ' Dummy for looping
  30.  
  31.     ' Set error handling
  32.     On Error Resume Next
  33.    
  34.     ' Set the targets to ping
  35.     strTarget(0,0) = "192.168.1.6"
  36.     strTarget(1,0) = "192.168.1.7"
  37.     strTarget(2,0) = "192.168.1.14"
  38.     strTarget(3,0) = "192.168.1.12"
  39.  
  40.     strTarget(0,1) = "TTSA"
  41.     strTarget(1,1) = "TTSB"
  42.     strTarget(2,1) = "TTSC"
  43.     strTarget(3,1) = "TTSD"
  44.  
  45.     Set objWSShell = WScript.CreateObject("WScript.Shell")
  46.  
  47.     ' Loop through all of the targets to ping
  48.     For i = 0 To UBound(strTarget)
  49.    
  50.         force_cScript()
  51.        
  52.         ' Ping the target
  53.         Set objWSExec = objWSShell.Exec("ping -n 2 -w 500 " & strTarget(i, 0)) ' Send 2 echo requests, waiting 0.5 seconds maximum
  54.        
  55.         ' Set 'strPingResultsFull' and 'strPingResultsPart' to an empty string
  56.         strPingResultsFull = ""
  57.         strPingResultsPart = ""
  58.        
  59.         ' Grab the results of the ping
  60.         Do Until objWSExec.StdOut.AtEndOfStream
  61.            
  62.             ' Grab the single line that is being looped
  63.             strSingleLine = objWSExec.StdOut.ReadLine
  64.            
  65.             ' Add the single line to the whole output, gradually creating 'strPingResultsFull'
  66.             strPingResultsFull = strPingResultsFull & strSingleLine
  67.            
  68.             'Check for the lines that should be output to the user and add them to 'strPingResultsPart'
  69.             If Instr(strSingleLine, "Ping statistics for") <> 0 Then
  70.                 strPingResultsPart = strSingleLine
  71.                 strPingResultsPart = Replace(strPingResultsPart, ":", " [" & strTarget(i, 1) & "]:", 1, 1)
  72.             End If
  73.             If Instr(strSingleLine, "Packets:") <> 0 Then
  74.                 strPingResultsPart = strPingResultsPart & vbCrLf & "    " & strSingleLine
  75.             End If
  76.            
  77.         Loop
  78.        
  79.         ' Check that there is something to output (there should be, even if the pings all fail)
  80.         If strPingResultsPart <> "" Then
  81.        
  82.             ' Add the ping results to the correct results strings
  83.             If InStr(strPingResultsFull, "Reply from") Then    
  84.                 strOutputSuccess = strOutputSuccess & strPingResultsPart & vbCrLf
  85.             Else
  86.                 strOutputFailure = strOutputFailure & strPingResultsPart & vbCrLf
  87.             End If
  88.            
  89.         End If
  90.        
  91.         Set objWSExec = Nothing
  92.        
  93.     Next
  94.  
  95.     Set objWSShell = Nothing
  96.    
  97. End Sub
  98.  
  99. '**
  100. ' Output the results to the screen
  101. '*
  102. Private Function output_results()
  103.  
  104.     Dim strResults  ' The results to output to the user
  105.  
  106.     If Len(strOutputSuccess) > 0 Then
  107.         strResults = "Successful results" & vbCrLf & "------------------" & vbCrLf & vbCrLf & strOutputSuccess
  108.     End If
  109.  
  110.     If Len(strOutputFailure) > 0 Then
  111.         If Len(strOutputSuccess) > 0 Then strResults = strResults & vbCrLf
  112.         strResults = "No reply from" & vbCrLf & "-------------" & vbCrLf & vbCrLf & strOutputFailure
  113.     End If
  114.  
  115.     If Len(strResults) > 0 Then
  116.         Msgbox strResults, vbOkOnly, "Ping Results"
  117.     Else
  118.         Msgbox "No results were found, indicating an error in the script. Please contact the author for support.", vbExclamation, "No Results Found"
  119.     End If
  120.  
  121. End Function
  122.  
  123. '**
  124. ' Force an application to run in a Command Script window, so that no additional windows are opened
  125. '*
  126. Private Function force_cScript()
  127.  
  128.      If InStr(UCase(WScript.FullName), "CSCRIPT.EXE") = 0 Then
  129.          Dim objShell : Set objShell = CreateObject("WScript.Shell")
  130.          objShell.Run "%comspec% /c cscript.exe " & Chr(34) & WScript.ScriptFullName & Chr(34), 0, False
  131.          WScript.Quit()
  132.      End If
  133.      
  134. End Function
  135.  
  136. '**
  137. ' Displays the progress box (in an Internet Explorer window)
  138. '
  139. ' @param required object 'objExplorer' The object that is to be the IE progress window
  140. '*
  141. Private Function display_progress(objExplorer)
  142.  
  143.     objExplorer.Navigate "about:blank"
  144.     objExplorer.ToolBar = 0
  145.     objExplorer.StatusBar = 0
  146.     objExplorer.Left = 600
  147.     objExplorer.Top = 374
  148.     objExplorer.Width = 400
  149.     objExplorer.Height = 152
  150.     objExplorer.Visible = 1
  151.    
  152.     Dim strStyle, strText, strButton
  153.    
  154.     strStyle = _
  155.     "<style>" & vbCrLf & _
  156.         "html, body{" & vbCrLf & _
  157.             "height: 116px;" & vbCrLf & _
  158.             "margin: 0;" & vbCrLf & _
  159.             "overflow: hidden;" & vbCrLf & _
  160.             "padding: 0;" & vbCrLf & _
  161.         "}" & vbCrLf & _
  162.         "#text{" & vbCrLf & _
  163.             "height: 50px;" & vbCrLf & _
  164.             "margin: 10px;" & vbCrLf & _
  165.         "}" & vbCrLf & _
  166.         "#buttons{" & vbCrLf & _
  167.             "background-color: #F0F0F0;" & vbCrLf & _
  168.             "height: 23px;" & vbCrLf & _
  169.             "padding: 10px;" & vbCrLf & _
  170.         "}" & vbCrLf & _
  171.         "input[type=""button""]{" & vbCrLf & _
  172.             "float: right;" & vbCrLf & _
  173.         "}" & vbCrLf & _
  174.         ".clear{" & vbCrLf & _
  175.             "clear: both;" & vbCrLf & _
  176.         "}" & vbCrLf & _
  177.     "</style>"
  178.     strText = "<div id=""text""><p>Please wait, servers are being pinged.</p><p>Results will be displayed as soon as they are ready.</p></div>"
  179.     strButton = "<div id=""buttons""><input type=""button"" name=""submit"" value=""Cancel"" onclick=""window.open('', '_self', ''); window.close();"" /><div class=""clear""></div></div>"
  180.    
  181.     objExplorer.Document.Body.Style.Font = "11pt 'Halvetica'"
  182.     objExplorer.Document.Body.Style.Cursor = "wait"
  183.     objExplorer.Document.Title = "Server ping script"
  184.     objExplorer.Document.Body.InnerHTML = strStyle & strText & strButton   
  185.    
  186. End Function
  187.  
  188. '**
  189. ' Closes the progress box (in an Internet Explorer window)
  190. '
  191. ' @param required object 'objExplorer' The object that the IE progress window
  192. '*
  193. Private Function close_progress(objExplorer)
  194.  
  195.     objExplorer.Document.Body.Style.Cursor = "default"
  196.     objExplorer.Quit
  197.  
  198. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement