SHARE
TWEET

PRTG Get-HTTPResonse

a guest Jul 8th, 2015 233 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. # - DefaultDNS: Use the DNS server that is configured as $DNS inside the script
  12. # - DNS: Use a DNS server of your choice
  13. # - Protocol: http or https are supported.
  14. # - HeaderHost: Modify the host variable in the header of the http request  
  15. # ------------------
  16. # (c) 2014 Stephan Linke | Paessler AG
  17.  
  18. param(
  19.     [string]$URL,
  20.     [int]$ResponseCode,
  21.     [string]$DefaultDNS = "false",
  22.     [string]$DNS = "",
  23.     [string]$protocol = "http",
  24.         [string]$HeaderHost = "www.example.com"
  25. )
  26.  
  27. # Set a default external DNS server
  28. if($DefaultDNS -eq "true"){ $DNS = "8.8.8.8" }
  29.  
  30. # Ignore SSL errors
  31. [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
  32.  
  33. # If a DNS is set, use it to resolve the URL
  34. if($DNS -ne ""){
  35.         $resolvedIP = Resolve-DnsName $URL -Server $DNS | Select IPAddress
  36.         $URL = $protocol+"://"+$resolvedIP[0].IPAddress+"/"
  37.         $req = [system.Net.WebRequest]::Create($URL)
  38.        
  39. }
  40. # ...otherwise use the given URL
  41. else{
  42.         $URL = $protocol+"://"+$URL+"/"
  43.  
  44.         $req = [system.Net.WebRequest]::Create($URL)
  45.        
  46. }
  47.  
  48. # Add some headers to the web request (for round robin dns and load balancers)
  49. $req.Host = $HeaderHost;
  50. $req.AllowAutoRedirect=$false
  51. # catch exceptions
  52. try                                                     { $res = $req.GetResponse() }
  53. catch [System.Net.WebException] { $res = $_.Exception.Response }
  54.  
  55.  
  56. # get the numeric response code of the request
  57. $Response = (($res.StatusCode) -as [int])
  58.  
  59. # and some error handling
  60. if([int]$Response -eq [int]$ResponseCode){
  61.     Write-Host $Response":Page returned HTTP code "$ResponseCode" as expected (used "$URL").";
  62.     exit 0;
  63. }
  64.  
  65. elseif([int]$Response -eq 0){
  66.         Write-Host $Response":Connection error (used "$URL").";
  67. }
  68.  
  69. else{
  70.     Write-Host $Response":Page returned "$Response" unexpectedly (used "$URL").";
  71.     exit 1;
  72. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top