Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for pingsite.vbs

(view source code of pingsite.vbs as plain text)

  1. Option Explicit
  2.  
  3. Dim strMsg, strWebsite
  4.  
  5. If WScript.Arguments.Unnamed.Count <> 1 Then Syntax
  6. If WScript.Arguments.Named.Count   <> 0 Then Syntax
  7. If Not IsHostName( WScript.Arguments.Unnamed(0) ) Then Syntax
  8.  
  9. strWebsite = WScript.Arguments.Unnamed(0)
  10. If PingSite( strWebsite ) Then
  11. 	WScript.Echo "Web site " & strWebsite & " is up and running!"
  12. Else
  13. 	WScript.Echo "Web site " & strWebsite & " is down!!!"
  14. End If
  15.  
  16.  
  17. Function PingSite( myWebsite )
  18. ' This function checks if a website is running by sending an HTTP request.
  19. ' If the website is up, the function returns True, otherwise it returns False.
  20. ' Argument: myWebsite [string] in "www.domain.tld" format, without the
  21. ' "http://" prefix.
  22. '
  23. ' Written by Rob van der Woude
  24. ' http://www.robvanderwoude.com
  25. 	Dim intStatus, objHTTP
  26.  
  27. 	Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
  28.  
  29. 	objHTTP.Open "GET", "http://" & myWebsite & "/", False
  30. 	objHTTP.SetRequestHeader "User-Agent", "Mozilla/4.0 (compatible; MyApp 1.0; Windows NT 5.1)"
  31.  
  32. 	On Error Resume Next
  33.  
  34. 	objHTTP.Send
  35. 	intStatus = objHTTP.Status
  36.  
  37. 	On Error Goto 0
  38.  
  39. 	Set objHTTP = Nothing
  40.  
  41. 	If intStatus = 200 Then
  42. 		PingSite = True
  43. 	Else
  44. 		PingSite = False
  45. 	End If
  46. End Function
  47.  
  48.  
  49. Function IsHostName( myString )
  50. ' Check if a string has a valid host name format
  51. 	Dim colMatches, objRE
  52.  
  53. 	Set objRE        = New RegExp
  54. 	objRE.Global     = False
  55. 	objRE.IgnoreCase = True
  56. 	objRE.Pattern    = "^([a-z][a-z_0-9-]+\.)*[a-z][a-z_0-9-]+\.[a-z]{2,8}$"
  57. 	Set colMatches   = objRE.Execute( myString )
  58.  
  59. 	If colMatches.Count = 1 Then
  60. 		IsHostName = True
  61. 	Else
  62. 		IsHostName = False
  63. 	End If
  64.  
  65. 	Set colMatches = Nothing
  66. 	Set objRE      = Nothing
  67. End Function
  68.  
  69.  
  70. Sub Syntax( )
  71. 		strMsg = "PingSite.vbs,  Version 1.01" & vbCrLf _
  72. 		       & "Check if a website is up and running" & vbCrLf & vbCrLf _
  73. 		       & "Usage:  PINGSITE.VBS  www.any_domain.tld" & vbCrLf & vbCrLf _
  74. 		       & "Written by Rob van der Woude" & vbCrLf _
  75. 		       & "http://www.robvanderwoude.com"
  76. 		WScript.Echo strMsg
  77. 		WScript.Quit 1
  78. End Sub
  79.  

page last modified: 2024-04-16; loaded in 0.0227 seconds