What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

How can I use PRTG to trace the route to a target?

Votes:

1

Is it possible to show the IP of the hops for routing to a target?

custom-script-exe custom-sensor exe-script-sensor hops ip prtg traceroute tracert

Created on Nov 14, 2014 12:44:19 PM by  Arne Seifert [Paessler Support]

Last change on Mar 16, 2015 5:07:58 PM by  Martina Wittmann [Paessler Support]



24 Replies

Accepted Answer

Votes:

1

This article applies to PRTG Network Monitor 14 or later

Monitoring the IP Addresses of Hops

PRTG already comes with a Traceroute Hop Count sensor out of the box. Due to its static channel architecture, it is not feasible to create a sensor with one channel for each hop. You can use a small Powershell script to show the route in the sensor status message.

While limited to the sensor status message, which is not part of the historic data, the following script used with an EXE/Script Sensor shows the IPs of the hops.

param( [string]$target ) $trace=tnc $target -TraceRoute $count=[string]$trace.TraceRoute.Count foreach ($ip in $trace.TraceRoute) { $result=$result+$ip+" > " } $result=$result.Substring(0,$result.Length-3) $message=$count+" hops to reach "+$target+" via "+$result write-host $count":"$message

System Requirements

  1. Windows 8.1 or newer, or Server 2012 R2 or newer.

How to Use this Script

  1. Enable Powershell RemoteSigned execution policy on your system with this command: set-executionpolicy RemoteSigned
  2. Create a file with the extension .ps1, for example, "trace.ps1"
  3. Copy the above source code, paste it into the file, and save it.
  4. Save this file into the PRTG installation folder, subfolder \Custom Sensor\EXE. Note: This requires local Windows administrator rights.
  5. In PRTG, add an EXE/Script sensor to a device. Pick trace.ps1 from the list of scripts.
  6. Use the parameter field to enter the target, for example, www.paessler.com
  7. Save the configuration.

Created on Nov 14, 2014 1:10:10 PM by  Arne Seifert [Paessler Support]

Last change on Jun 16, 2015 12:55:33 PM by  Arne Seifert [Paessler Support]



Votes:

0

Hello support,

The result of sensor is :

"hops to reach www.paessler.com via"

But nothing else.

Also the sensor is green and the value is 0.

Can you help me?

Created on May 29, 2015 2:54:41 PM



Votes:

0

Here is the result of Sensor log:

---

Le terme « tnc » n'est pas reconnu comme nom d'applet de commande, fonction, fi
chier de script ou programme exécutable. Vérifiez l'orthographe du nom, ou si u
n chemin d'accès existe, vérifiez que le chemin d'accès est correct et réessaye
z.
Au niveau de D:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXE\tra
cert.ps1 : 4 Caractère : 11
+ $trace=tnc <<<<  $target -TraceRoute
    + CategoryInfo          : ObjectNotFound: (tnc:String) [], CommandNotFound 
   Exception
    + FullyQualifiedErrorId : CommandNotFoundException
 
: hops to reach www.paessler.com via 

---

Created on May 29, 2015 2:57:41 PM

Last change on Jun 1, 2015 9:09:11 AM by  Arne Seifert [Paessler Support]



Votes:

0

Dear Yannick

I am sorry, your Windows seems to lack the tnc utility which was introduced with Windows 8.1 (or Server 2012 R2) as part of Powershell 4.0.

edit: I put a clarification in the original article.

Created on Jun 1, 2015 9:12:21 AM by  Arne Seifert [Paessler Support]

Last change on Jun 1, 2015 9:14:26 AM by  Arne Seifert [Paessler Support]



Votes:

0

@Paessler Support
Thanks for the aboving script ,that is fine for trace the hops to destination.

I have one more question on this, how could I define the related script that could trace the PING reply times, such like bellowing "PingReplyDetails (RTT) : 105 ms"
PS C:\Windows\system32> tnc 8.8.8.8 -traceroute

ComputerName           : 8.8.8.8
RemoteAddress          : 8.8.8.8
InterfaceAlias         : Ethernet 4
SourceAddress          : 10.204.17.185
PingSucceeded          : True
PingReplyDetails (RTT) : 105 ms
TraceRoute             : 10.204.16.1
                         59.125.99.235
                         59.125.99.254
                         168.95.228.58
                         220.128.1.82
                         220.128.8.81
                         220.128.8.173
                         72.14.205.102
                         209.85.243.30
                         216.239.46.223
                         216.239.43.101
                         TimedOut
                         8.8.8.8

I already adjusted the the script and put it in the path of advanced script folder like bellowing for add 2 channels which including hops and response times rather than execution time, but it always returned "invalid point operation"

param(
  [string]$target
)
$trace=tnc $target -TraceRoute
$count=[string]$trace.TraceRoute.Count
$time=[string]$trace.PingReplyDetails
foreach ($ip in $trace.TraceRoute) {
$result=$result+$ip+" > "
}
$result=$result.Substring(0,$result.Length-3)
$message=$count+" hops to reach "+$target+" via "+$result
#write-host $count":"$message
#write-host $time
Write-Host '<prtg>'
Write-Host "<result>" 
"<channel>Response</channel>" 
"<value>"+ $time.Seconds +"</value>" 
"</result>"
Write-Host "<result>" 
"<channel>Hops</channel>" 
"<value>"+ $count +"</value>" 
"</result>"
Write-Host "</prtg>"

Does any support can help me on this ?
Thanks in advance.

Created on Jul 29, 2015 3:01:45 AM

Last change on Jul 29, 2015 9:06:23 AM by  Arne Seifert [Paessler Support]



Votes:

0

Dear Kelvin Xue

Our scripts provide a demonstration which can be expanded by users; however we offer no technical support for these scripts.

Please check what happens when you run the script from the command line. If the error appears in a particular line of code, please try to correct that line.

If you can run the script from the command line, but not as sensor, please open the sensor configuration and set the security context to use the credentials provided by the parent device. In addition, please open the settings tab of the according device an enter the Windows credentials the script should use.

Created on Jul 29, 2015 9:09:14 AM by  Arne Seifert [Paessler Support]



Votes:

0

works on windows7 or windows 2008 r2

param(
  [string]$target
)
$target ="bso.at"
$trace = tracert -4 $target
$count = 0
$result = ""
foreach ($line in $trace) {
    if($line.contains(" ms") -or $line.contains(" * ") ) {
        $count = $count + 1
        $array = $line.trim() -split "\s+"
        $ip =  $array[7] + " " + $array[8]
        $result = $result + $ip.trim() + " > "
    }
}
$result = $result.Substring(0, $result.Length - 3)
$message = "$count" + " hops to reach " + $target + " via " + $result
write-host $count":"$message

Created on Nov 3, 2015 11:22:51 AM



Votes:

2

Sometimes you want to check which route is in use, e.g. there is a default and a failoder route:

# $Destination: IP/DNS
# $DefaultIP:	x.x.x.x, x.x.*.* IP within the default route
# $FailoverIP:	y.y.y.y, y.y.*.* IP within the failover route

param(
[string]$Destination,[string]$DefaultIP,[string]$FailoverIP
)

$CountDefaultIP = 0
$CountFailoverIP = 0
$Sum = 0

$Trace = Test-NetConnection $Destination -TraceRoute
$TraceCount = $Trace.TraceRoute.Count

For ($i = 0; $i -le $TraceCount-1; $i++) {
    $TraceValue = $Trace.TraceRoute.GetValue($i)
    If($TraceValue -like $DefaultIP) {
        $CountDefaultIP = $CountP1 + 1
		$Sum = 1		
    }
    If($TraceValue -like $FailoverIP) {
        $CountFailoverIP = 1
		$Sum = 2
    }
}

write-host "<?xml version=`"1.0`" encoding=`"UTF-8`"?>"
write-host "<prtg>"
write-host "<result>"
write-host "<channel>Route Status </channel>"
write-host "<value>$Sum</value>"
write-host "<LimitMode>1</LimitMode>"
write-host "<LimitMinError>0.5</LimitMinError>"
write-host "<LimitMaxWarning>1.5</LimitMaxWarning>"
write-host "</result>"
write-host "<result>"
write-host "<channel>Default $DefaultIP </channel>"
write-host "<value>$CountDefaultIP</value>"
write-host "<LimitMode>1</LimitMode>"
write-host "<LimitMinWarning>0.5</LimitMinWarning>"
write-host "</result>"
write-host "<result>"
write-host "<channel>Failover $FailoverIP </channel>"
write-host "<value>$CountFailoverIP</value>"
write-host "<LimitMode>1</LimitMode>"
write-host "<LimitMaxWarning>0.5</LimitMaxWarning>"
write-host "</result>"
write-host "</prtg>"

Created on Nov 12, 2015 4:31:49 PM



Votes:

0

Sadly, both last script do not work for me on Windows Server 2012, could you or someone post the updates please?

I need to track overloaded machines on a trace to a destinations.

I am surprised that such a standard sensor like a qualified traceroute is not covered in standard sensors by Paessler.

Thanks in advance...peter

Created on Feb 6, 2017 3:00:42 PM



Votes:

0

Dear Support,

Can i get the history of below Script, For example now it's showing me the hop count and the trace route to the destination, I want to log the trace if the route changes to get like history or something show me that route changes ? is is possible through this Script ?

Created on Mar 4, 2019 9:57:56 AM



Votes:

0

Dear amro-shoufi,

please understand that custom scripts, even if posted by ourselves, are not covered by further technical support. The script itself does no logging or comparison with earlier traceroute runs. You might be able to add additional features.

Created on Mar 5, 2019 1:16:11 PM by  Arne Seifert [Paessler Support]

Last change on Mar 5, 2019 1:19:50 PM by  Arne Seifert [Paessler Support]



Votes:

6

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

Created on Mar 6, 2019 4:36:41 AM



Votes:

0

I do This Set power Shell to powershell set-executionpolicy RemoteSigned in exe script pickup trace.ps1 and set paramater 8.8.8.8

But see theis error

Response not wellformed: "(File C:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXE\trace.ps1 c annot be loaded because the execution of scripts is disabled on this system. Pl ease see "get-help about_signing" for more details. At line:1 char:2 + & <<<< 'C:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXE\trace. ps1' 8.8.8.8; exit $LASTEXITCODE + CategoryInfo : NotSpecified: (:) [], PSSecurityException + FullyQualifiedErrorId : RuntimeException )" (code: PE132)

Os:windows 7

Created on Jul 15, 2019 7:33:28 AM



Votes:

0

Hi there,

Please make sure that you set it explicitly in the 32-Bit PowerShell, per default the 64-bit version is opened under Windows. Does it work afterwards?

best regards.

Created on Jul 15, 2019 10:18:43 AM by  Dariusz Gorka [Paessler Support]



Votes:

0

Hey Florian, great Script! Helped me a lot.

Would it be possible for you to edit the script to check for an explicit IP address on the trace? For example if IP adress A is somewhere in the traceroute, the Sensor is green and if IP adress B is somewhere in the traceroute then the Sensor will change to Error.

In this way i could track if the traffic will use the main MPLS line or the Backup line.

Thanks.

Best regards

Created on Sep 11, 2020 9:10:58 AM



Votes:

2

Did write version 2 of the script for this.. see below.

Now you have 3x parameter, the third one is an expected IP address that has to be found on the route. If this is not the case, the sensor would alert you.

Hope this helps - I did not test it with PRTG, just really quick with PowerShell. How ever, because I introduced a new channel, it can be that you need to create new sensors. It is possible that it works by just exchanging the script, but not sure. In any case, add the third parameter to the sensor.

param(
  [string]$Target,
  [string]$ExpectedRoute,
  [string]$ExpectedIP
)

$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;
    }
}

$ExpectedIPFound = 0
ForEach ($IP in $Trace.TraceRoute) {
	If ($IP.Trim() -eq $ExpectedIP.Trim()) {
		$ExpectedIPFound = 1;
		break;
	}
}

$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>Expected IP Found</channel>
        <value>$ExpectedIPFound</value>
        <LimitMode>1</LimitMode>
        <LimitMinError>1</LimitMinError>		
    </result>	
    <result>
        <channel>Route matches expected route</channel>
        <value>$asexpected</value>
        <LimitMode>1</LimitMode>
        <LimitMinError>1</LimitMinError>
        <LimitErrorMsg>$Message</LimitErrorMsg>
    </result>
	<text>$Message</text>
</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"

Regards

Florian Rossmark

www.it-admins.com

Created on Sep 11, 2020 1:41:29 PM



Votes:

0

Big hats-off to Florian!

A manual option would be to use the Traceroute option in the device tools.

Created on Sep 11, 2020 2:09:54 PM by  Arne Seifert [Paessler Support]



Votes:

0

Great! Many thanks. Will test it now with PRTG.

Created on Sep 14, 2020 6:37:40 AM



Votes:

0

How to add code in PRTG?

param(
[string]$target
)
$trace=tnc $target -TraceRoute
$count=[string]$trace.TraceRoute.Count
foreach ($ip in $trace.TraceRoute) {
$result=$result+$ip+" > "
}
$result=$result.Substring(0,$result.Length-3)
$message=$count+" hops to reach "+$target+" via "+$result
write-host $count":"$message

Created on Dec 15, 2020 9:29:20 PM

Last change on Dec 16, 2020 3:32:31 PM by  Arne Seifert [Paessler Support]



Votes:

0

Hello,

such sensor could be added as EXE/Script sensor once you put the file into "C:\Program Files (x86)\PRTG Network Monitor\Custom Sensors\EXE".

Created on Dec 16, 2020 3:34:44 PM by  Arne Seifert [Paessler Support]



Votes:

0

Hello @Florian,

I have 9 hops and usually my second hope changes when the ISP is down. Would it be possible to check if these hope includes the gateway(second IP), if not get notification with this script?

Thanks. Izac

Created on Apr 29, 2021 12:46:41 PM



Votes:

0

@Izac,

Of course this is possible - it's actually the whole purpose of the script.

Parameter 1 (Target) - define a target IP address for the route
Parameter 2 (ExpectedRoute) - comma separated, any IP expected on this route, including the target
Parameter 3 (ExpectedIP) - a single IP that HAS to be in the route

Sample:

TRACERT 10.99.99.99
- hop 1 - 192.168.0.1
- hop 2 - 192.168.54.28
- hop 3 - 172.25.82.34
- hop 4 - 10.2.5.6
- hop 5 - 10.99.99.99

Your Parameters:

Parameter 1 - 10.99.99.99
Parameter 2 - 192.168.0.1,192.168.54.28,172.25.82.34,10.2.5.6,10.99.99.99
Parameter 3 - 172.25.82.34

Why Parameter 3? Similar to your case, you always expect to see 172.25.82.34 on this route, if not something is wrong..

If you don't need parameter 3 - just use version 1 of the script and use the first two parameter..

Hope this helps you.

Regards Florian

Created on Apr 29, 2021 1:36:47 PM

Last change on Apr 29, 2021 6:55:49 PM by  Felix Wiesneth [Paessler Support]



Votes:

0

Hi Dear i Used This Script

param(
  [string]$target
)
$target ="bso.at"
$trace = tracert -4 $target
$count = 0
$result = ""
foreach ($line in $trace) {
    if($line.contains(" ms") -or $line.contains(" * ") ) {
        $count = $count + 1
        $array = $line.trim() -split "\s+"
        $ip =  $array[7] + " " + $array[8]
        $result = $result + $ip.trim() + " > "
    }
}
$result = $result.Substring(0, $result.Length - 3)
$message = "$count" + " hops to reach " + $target + " via " + $result
write-host $count":"$message

Install Power Shell and Add Exe Sensor on prt with this script Sensor run But show my only run time

Any tracepath Donnot Exsist in the Sensor

Please help me?

Created on Jul 31, 2021 7:31:28 AM

Last change on Aug 2, 2021 6:51:08 AM by  Felix Wiesneth [Paessler Support]



Votes:

0

Hello,

for analysis, please open the sensor's "Settings" tab and enable the "Store Result" option.

After the next sensor scan, the result logs are written to "C:\ProgramData\Paessler\PRTG Network Monitor\Logs\sensors" on the respective probe. Please check those, if the script throws errors.

Created on Aug 6, 2021 2:45:14 PM by  Arne Seifert [Paessler Support]




Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.