Contact Info

Crumbtrail

ActiveXperts.com » Network Monitor » Scripts » Custom Script

traceroute.ps1 - powershell script by ActiveXperts Software

traceroute.ps1 tracks the route of packets from one IP to another.

Use traceroute.ps1 directly from ActiveXperts Network Monitor; in the Manager's 'Monitor' menu, select 'New Check (Script)' and select traceroute.ps1. Configure the required parameter, or press 'Load a working sample'.

In ActiveXperts Network Monitor, Administrators can use three different scripting languages: Powershell, VBScript and SSH.


traceroute.ps1 script code

#################################################################################
# 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
}