Advertisement
DGorka

ResponseCode

Aug 29th, 2016
2,604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ___ ___ _____ ___
  2. #| _ \ _ \_   _/ __|
  3. #|  _/   / | || (_ |
  4. #|_| |_|_\ |_| \___|
  5. #    NETWORK MONITOR
  6. #-------------------
  7. # Description: The script will check if a URL returns the expected return code and alert you if it doesn't
  8. # Parameters:
  9. # - URL: The site you want to check. Format: www.google.de
  10. # - ResponseCode: The expected numerical response code (overview: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
  11. # - Protocol: http or https are supported.
  12. # ------------------
  13. # (c) 2016 Stephan Linke | Paessler AG
  14.  
  15. param(
  16.     [string]$URL = "www.google.de",
  17.        [int]$ResponseCode = 200,
  18.     [string]$protocol = "http"
  19. )
  20.  
  21. # Ignore SSL errors
  22. [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
  23.    
  24. # ...otherwise use the given URL
  25. $URL = $protocol+"://"+$URL+"/"
  26. $req = [system.Net.WebRequest]::Create($URL)
  27.  
  28. # catch exceptions
  29. try
  30. {
  31.     # retrieve the response cod
  32.     [int]$Response = ($req.GetResponse()).StatusCode
  33.    
  34.     if([int]$Response -eq [int]$ResponseCode)
  35.     { Write-Host $Response":Page returned HTTP code "$ResponseCode" as expected (used "$URL")."; }
  36.     else
  37.     { Write-Host $Response":Page returned "$Response" instead of $ResponseCode unexpectedly (used "$URL")."; exit 2; }
  38.    
  39. }
  40. catch [System.Net.WebException] {
  41.     $res = ($_.Exception.Message -match '\d+');
  42.     $Response = $Matches[0]
  43.  
  44.     if([int]$Response -eq [int]$ResponseCode)
  45.     { Write-Host $Response":Page returned HTTP code "$ResponseCode" as expected (used "$URL")."; }
  46.     else
  47.     { Write-Host $Response":Page returned "$Response" instead of $ResponseCode unexpectedly (used "$URL")."; exit 2; }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement