traceroute how to use code example

Example 1: traceroute

tracert [ipadress]

Example 2: what is traceroute

Traceroute is a network diagnostic tool used to track 
in real-time 
the pathway taken by a packet on an IP network from source to destination, 
reporting the IP addresses of all the routers it pinged in between. 
Traceroute also records the time taken for each hop the packet makes \
during its route to the destination.

Example 3: Traceroute

#################################################################################
# ActiveXperts Network Monitor PowerShell script, © ActiveXperts Software B.V.
# For more information about ActiveXperts Network Monitor, visit the ActiveXperts 
# Network Monitor web site at http://www.activexperts.com
#################################################################################
# Script
#     TraceRoute.ps1
# Description: 
#     Tracks the route packets taken from an IP network on their way to a given host.
#     This function uses ActiveXperts Network Component.
#     ActiveXperts Network Component is automatically licensed when ActiveXperts Network Monitor is purchased.
#     For more information about ActiveXperts Network Component, see: www.activexperts.com/network-component
# Declare Parameters:
#     1) strHost (string) - Host name or IP address or target
#     2) nMaxHops (int) - Maximum number of hops to search for target
#     3) nMaxResponseTime (int) - Wait timeout milliseconds for each reply
# Usage:
#     .\TraceRoute.ps1 '<Hostname | IP>' <MaximumHops> <MaximumResponseTimeMsecs>
# Sample:
#     .\TraceRoute.ps1 'www.activexperts.com' 30 500
#################################################################################

# -- Declare Parameters
param( [string]$strHost = '', [int]$nMaxHops = '', [int]$nMaxResponseTime = '' )

# -- Use _activexperts.ps1 with common functions 
. 'C:\Program Files\ActiveXperts\Network Monitor\Scripts\Monitor (ps1)\_activexperts.ps1' 


#################################################################################
# // --- Main script ---
#################################################################################

# -- Clear screen and clear error
cls
$Error.Clear()

# -- Validate parameters, return on parameter mismatch
if( $strHost -eq '' -or $nMaxHops -eq '' -or $nMaxResponseTime -eq '' )
{
  $res = 'UNCERTAIN: Invalid number of parameters - Usage:  .\TraceRoute.ps1 "<Hostname | IP>" <MaximumHops> <MaximumResponseTimeMsecs>'
  echo $res
  exit
}


# Create a instance
$objTraceRt  = new-object -comobject AxNetwork.TraceRoute

$objTraceRt.ResolveHostName = $True
$objTraceRt.MaxHops = $nMaxHops
$objTraceRt.Timeout = $nMaxResponseTime

$nHops = 0

$objHop = $objTraceRt.FindFirstHop( $strHost )

if( $objTraceRt.LastError -ne 0 )
{
  $res = 'ERROR: Route to [' + $strHost + '] not found; result=[' + $objTraceRt.LastError + ': ' + $objTraceRt.GetErrorDescription( $objTraceRt.LastError ) + '] DATA:0'
  echo $res
  exit
}

do
{ 
  if( $objHop.Host -ne '' )
  {
    $strHopName = $objHop.Host
  }
  else
  {
    $strHopName = $objHop.IP
  }
  
  $nHops = $nHops + 1
  
  if( $objHop.ResponseTime -gt $nMaxResponseTime )
  {
    $res = 'ERROR: Hop [' +  $strHopName + '] response timeout [' + $nMaxResponseTime + 'ms] exceeded, time=[' + $objHop.ResponseTime + 'ms] DATA:' + $nHops 
    echo $res
    exit
  }

  $objHop = $objTraceRt.FindNextHop()

} while( $objTraceRt.LastError -eq 0 )

$res = 'SUCCESS: TraceRoute succeeded, all [' + $nHops + '] hops replied within [' + $objTraceRt.Timeout + 'ms] DATA:' + $nHops 
echo $res
exit


#################################################################################
# // --- Catch script exceptions ---
#################################################################################

trap [Exception]
{
  $res = 'UNCERTAIN: ' + $_.Exception.Message
  echo $res
  exit
}

Tags:

Misc Example