Invested 30 minutes and created the below script:
param(
[string]$Target,
[string]$ExpectedRoute
)
$ER = $ExpectedRoute.Split(",");
$Trace=TNC $Target -TraceRoute;
$Count=[string]$Trace.TraceRoute.Count;
$Message = "";
$asexpected = 1;
[int]$i = 0;
ForEach ($IP in $Trace.TraceRoute) {
If ($i -lt $ER.Length){
If ($IP.Trim() -ne $ER[$i].Trim()) {
$Message = "Route change at HOP #" + ($i + 1).ToString() + " - IP expected: " + $ER[$i] + " - IP found: $IP";
$asexpected = 0;
break;
}
}
$i += 1;
}
If ($Trace.TraceRoute.Count -ne $ER.Length) {
If ($asexpected -eq 1) {
$Message = "Route changed - expected HOP count: " + $ER.Length + " - detected HOP count: " + $Trace.TraceRoute.Count;
$asexpected = 1;
}
}
$reached = 0;
If ($Trace.PingSucceeded) {
$reached = 1;
}
$XML = "<prtg>
<result>
<channel>Target reached</channel>
<value>$reached</value>
<LimitMode>1</LimitMode>
<LimitMinError>1</LimitMinError>
</result>
<result>
<channel>Round Trip Time</channel>
<value>" + $Trace.PingReplyDetails.RoundtripTime + "</value>
</result>
<result>
<channel>Total HOPs</channel>
<value>" + $Trace.TraceRoute.Count + "</value>
</result>
<result>
<channel>Route matches expected route</channel>
<value>$asexpected</value>
<LimitMode>1</LimitMode>
<LimitMinError>1</LimitMinError>
<LimitErrorMsg>$Message</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"
What you need as parameter is a target IP or DNS name - and a list of expected HOPs in between. The fact that you need to inject the expected HOPs bypasses that you would need some kind of a config-file or database.
It would not work if you write out a log-file - cause this log would only temporarily cause an error state and by the next check possible become the new norm.
Example for the parameters... please inject first the target host/IP and then a list of HOPs - comma separated - as IP addresses, including the target IP address.
10.1.1.1 "192.168.1.254,10.0.0.1,10.1.0.1,10.1.1.1"
This will actually do the following:
- report back if the target was reachable
- tell you the total roundtrip time to the target
- show you the HOP count that was true (right now) towards the target
- check if each of the injected IP HOPs stayed the same
- if so no further message is shown
- if a HOP is different, this will raise an error and report which HOP was found and which was expected
- if the injected HOPs are all found but the amount was different, an error is raised and you see the difference of the HOP counts
This should help everyone with this challenge... the script might be able to be optimized, but it should be a rather good start...
Regards
Florian Rossmark
www.it-admins.com
Add comment