14

I'm using PSVersion 2.0 and I was wondering is there a equivalent to the traceroute for it?

I'm aware that on PowerShell v4 there is Test-NetConnection cmdlet to do tracert but v2?! It can be done like:

Test-NetConnection "IPaddress/HOSTaname" -TraceRoute

Thanks

Matt
  • 40,384
  • 7
  • 62
  • 97
Sylca
  • 2,385
  • 4
  • 26
  • 51

4 Answers4

27

As mentioned in the comment, you can make your own "poor-mans-PowerShell-tracert" by parsing the output from tracert.exe:

function Invoke-Tracert {
    param([string]$RemoteHost)

    tracert $RemoteHost |ForEach-Object{
        if($_.Trim() -match "Tracing route to .*") {
            Write-Host $_ -ForegroundColor Green
        } elseif ($_.Trim() -match "^\d{1,2}\s+") {
            $n,$a1,$a2,$a3,$target,$null = $_.Trim()-split"\s{2,}"
            $Properties = @{
                Hop    = $n;
                First  = $a1;
                Second = $a2;
                Third  = $a3;
                Node   = $target
            }
            New-Object psobject -Property $Properties
        }
    }
}

By default, powershell formats objects with 5 or more properties in a list, but you can get a tracert-like output with Format-Table:

enter image description here

Mathias R. Jessen
  • 106,010
  • 8
  • 112
  • 163
4

Fixed a few bugs in " Mid-Waged-Mans-Tracert" Version, modularized it, and added some customization pieces. @MrPaulch had a great PoC.

function Invoke-Traceroute{
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,Position=1)]
        [string]$Destination,

        [Parameter(Mandatory=$false)]
        [int]$MaxTTL=16,

        [Parameter(Mandatory=$false)]
        [bool]$Fragmentation=$false,

        [Parameter(Mandatory=$false)]
        [bool]$VerboseOutput=$true,

        [Parameter(Mandatory=$false)]
        [int]$Timeout=5000
    )

    $ping = new-object System.Net.NetworkInformation.Ping
    $success = [System.Net.NetworkInformation.IPStatus]::Success
    $results = @()

    if($VerboseOutput){Write-Host "Tracing to $Destination"}
    for ($i=1; $i -le $MaxTTL; $i++) {
        $popt = new-object System.Net.NetworkInformation.PingOptions($i, $Fragmentation)   
        $reply = $ping.Send($Destination, $Timeout, [System.Text.Encoding]::Default.GetBytes("MESSAGE"), $popt)
        $addr = $reply.Address

        try{$dns = [System.Net.Dns]::GetHostByAddress($addr)}
        catch{$dns = "-"}

        $name = $dns.HostName

        $obj = New-Object -TypeName PSObject
        $obj | Add-Member -MemberType NoteProperty -Name hop -Value $i
        $obj | Add-Member -MemberType NoteProperty -Name address -Value $addr
        $obj | Add-Member -MemberType NoteProperty -Name dns_name -Value $name
        $obj | Add-Member -MemberType NoteProperty -Name latency -Value $reply.RoundTripTime

        if($VerboseOutput){Write-Host "Hop: $i`t= $addr`t($name)"}
        $results += $obj

        if($reply.Status -eq $success){break}
    }

    Return $results
}
3

I must admit I wanted to see whether someone already did this.

You can use the .Net Framework to implement a not-so-poor-mans-traceroute as a Powershell Script

Here a primer, that works fast, but dangerous. Also, no statistics.

# 
# Mid-Waged-Mans-Tracert
#

$ping = new-object System.Net.NetworkInformation.Ping
$timeout = 5000
$maxttl  = 64
$address = [string]$args
$message = [System.Text.Encoding]::Default.GetBytes("MESSAGE")
$dontfragment = false
$success = [System.Net.NetworkInformation.IPStatus]::Success

echo "Tracing $address"
for ($ttl=1;$i -le $maxttl; $ttl++) {
    $popt = new-object System.Net.NetworkInformation.PingOptions($ttl, $dontfragment)   
    $reply = $ping.Send($address, $timeout, $message, $popt)


    $addr = $reply.Address
    $rtt = $reply.RoundtripTime
    try {
        $dns = [System.Net.Dns]::GetHostByAddress($addr)
    } catch {
        $dns = "-"
    }

    $name = $dns.HostName

    echo "Hop: $ttl`t= $addr`t($name)"
    if($reply.Status -eq $success) {break}
}

Edit:

Removed some of the danger by adding a catch statement. The only danger that is still present is the fact that we only send a single request per hop, which could mean that we don't reach a hop due to a innocent package drop. Resolving that issue remains a readers exercise. Hint: (Think of loops within loops)

Bonus: We now attempt to get the dns entry of each hop!

MrPaulch
  • 1,320
  • 15
  • 19
  • Nice "native" approach, awesome! Maybe explain the "danger" involved? :-) – Mathias R. Jessen Feb 11 '16 at 20:06
  • 1
    Well the danger involved, was the lack of redundancy (if a single icmp message get's lost we don't get the result for that particular hop at all) + we didn't catch the PingException. – MrPaulch Feb 12 '16 at 09:58
2

With at least PS 5 you can

Test-Netconnection stackoverflow.com -TraceRoute

8DH
  • 1,712
  • 23
  • 32