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).
- # - DefaultDNS: Use the DNS server that is configured as $DNS inside the script
- # - DNS: Use a DNS server of your choice
- # - Protocol: http or https are supported.
- # - HeaderHost: Modify the host variable in the header of the http request
- # ------------------
- # (c) 2014 Stephan Linke | Paessler AG
- param(
- [string]$URL,
- [int]$ResponseCode,
- [string]$DefaultDNS = "true",
- [string]$DNS = "",
- [string]$protocol = "http",
- [string]$HeaderHost = "www.example.com"
- )
- # Set a default external DNS server
- if($DefaultDNS -eq "true"){ $DNS = "8.8.8.8" }
- # Ignore SSL errors
- [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
- # If a DNS is set, use it to resolve the URL
- if($DNS -ne ""){
- $resolvedIP = Resolve-DnsName $URL -Server $DNS | Where-Object Type -eq A | Select IPAddress
- $URL = $protocol+"://"+$resolvedIP[0].IPAddress+"/"
- $req = [system.Net.WebRequest]::Create($URL)
- }
- # ...otherwise use the given URL
- else{
- $URL = $protocol+"://"+$URL+"/"
- $req = [system.Net.WebRequest]::Create($URL)
- }
- # Add some headers to the web request (for round robin dns and load balancers)
- $req.Host = $HeaderHost;
- $req.AllowAutoRedirect=$false
- # catch exceptions
- try { $res = $req.GetResponse() }
- catch [System.Net.WebException] { $res = $_.Exception.Response }
- # get the numeric response code of the request
- $Response = (($res.StatusCode) -as [int])
- # and some error handling
- if([int]$Response -eq [int]$ResponseCode){
- Write-Host $Response":Page returned HTTP code "$ResponseCode" as expected (used "$URL").";
- exit 0;
- }
- elseif([int]$Response -eq 0){
- Write-Host $Response":Connection error (used "$URL").";
- }
- else{
- Write-Host $Response":Page returned "$Response" instead of $ResponseCode unexpectedly (used "$URL").";
- exit 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement