The following is not yet the perfect script - but it might just work for you.
param(
[string] $DomainName = "",
[string] $IP = "",
[string] $KeyWord = " ",
[int] $MinimumResponseSizeInByte = 1,
[bool] $useHTTPS = $true
)
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
If ($useHTTPS -eq $true) {
$Prefix = "https://"
} Else {
$Prefix = "http://"
}
$IWRResponse = "";
$IWRError = 0;
$IWRErrorMessage = "";
$IWRResponse = Try {
Invoke-WebRequest -Uri "$Prefix$IP" -Headers @{Host="$DomainName"} -ErrorAction Stop
} Catch {
$IWRError = $_.Exception.GetHashCode()
If ($IWRError -eq 0) {
$IWRError = 1
}
$IWRErrorMessage = $_.Exception.Message
}
Try {
$IWRResponse = $IWRResponse.ToString()
} Catch {}
$KeyWordFound = 0
Try {
If ($IWRResponse.Contains($KeyWord)) {
$KeyWordFound = 1
}
} Catch {}
$ResponseSufficient = 0
Try {
If ($IWRResponse -gt $MinimumResponseSizeInByte) {
$ResponseSufficient = 1
}
} Catch {}
$XML = "<prtg>
<result>
<channel>Check result</channel>
<value>$IWRError</value>
<LimitMode>1</LimitMode>
<LimitMinError>0</LimitMinError>
<LimitMaxError>0</LimitMaxError>
<LimitErrorMsg>$IWRErrorMessage</LimitErrorMsg>
</result>
<result>
<channel>Keyword found</channel>
<value>$KeyWordFound</value>
<LimitMode>1</LimitMode>
<LimitMinError>1</LimitMinError>
<LimitErrorMsg>Keyword was not found in response</LimitErrorMsg>
</result>
<result>
<channel>Response Length</channel>
<value>" + $IWRResponse.Length + "</value>
<VolumeSize>Byte</VolumeSize>
<NotifyChanged>1</NotifyChanged>
</result>
<result>
<channel>Reponse Length sufficient</channel>
<value>$ResponseSufficient</value>
<LimitMode>1</LimitMode>
<LimitMinError>1</LimitMinError>
<LimitErrorMsg>Response size not sufficient</LimitErrorMsg>
</result>
</prtg>"
Function WriteXmlToScreen ([xml]$xml) #just to make it clean XML code...
{
$StringWriter = New-Object System.IO.StringWriter;
$XmlWriter = New-Object System.Xml.XmlTextWriter $StringWriter;
$XmlWriter.Formatting = "indented";
$xml.WriteTo($XmlWriter);
$XmlWriter.Flush();
$StringWriter.Flush();
Write-Output $StringWriter.ToString();
}
WriteXmlToScreen "$XML"
In you case the following parameters would possibly work
-DomainName "app.test.local" -IP "192.168.10.10/login.html" -useHTTPs 0
If you add -Keyword "abcd" you can search for a keyword in the raw HTML response.
You could also engage the -MinimumResponseSizeInByt 999 parameter - while 999 bytes is just a guess of how much you should get back...
Let me know if it worked... of course you would need a sensor per target IP in this case - I did not develop it out to the extreme and the code could easily be optimized, just thought the idea of it is nice and it might be a start.
Regards
Florian Rossmark
www.it-admins.com
Add comment