Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ___ ___ _____ ___
- #| _ \ _ \_ _/ __|
- #| _/ / | || (_ |
- #|_| |_|_\ |_| \___|
- # NETWORK MONITOR
- #-------------------
- # Description: The script will check if a URL returns the expected return code and alert you if it doesn't
- # Parameters:
- # - URL: The site you want to check. Format: www.google.de
- # - ResponseCode: The expected numerical response code (overview: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
- # - Protocol: http or https are supported.
- # ------------------
- # (c) 2016 Stephan Linke | Paessler AG
- param(
- [string]$URL = "www.google.de",
- [int]$ResponseCode = 200,
- [string]$protocol = "http"
- )
- # Ignore SSL errors
- [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
- # ...otherwise use the given URL
- $URL = $protocol+"://"+$URL+"/"
- $req = [system.Net.WebRequest]::Create($URL)
- # catch exceptions
- try
- {
- # retrieve the response cod
- [int]$Response = ($req.GetResponse()).StatusCode
- if([int]$Response -eq [int]$ResponseCode)
- { Write-Host $Response":Page returned HTTP code "$ResponseCode" as expected (used "$URL")."; }
- else
- { Write-Host $Response":Page returned "$Response" instead of $ResponseCode unexpectedly (used "$URL")."; exit 2; }
- }
- catch [System.Net.WebException] {
- $res = ($_.Exception.Message -match '\d+');
- $Response = $Matches[0]
- if([int]$Response -eq [int]$ResponseCode)
- { Write-Host $Response":Page returned HTTP code "$ResponseCode" as expected (used "$URL")."; }
- else
- { Write-Host $Response":Page returned "$Response" instead of $ResponseCode unexpectedly (used "$URL")."; exit 2; }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement