-1

Tracert output:

 1     4 ms     5 ms     3 ms  192.168.32.254
 2    <1 ms    <1 ms    <1 ms  192.168.39.238
 3     1 ms     1 ms     1 ms  10.88.8.122
 4     2 ms     2 ms     1 ms  10.88.234.70
 5     1 ms     1 ms     1 ms  10.88.246.137
 6     1 ms     1 ms     1 ms  10.88.247.161
 7     3 ms     4 ms     3 ms  89.146.116.133
 8    15 ms    15 ms    16 ms  89.146.105.177
 9    18 ms    18 ms    18 ms  89.146.105.178
10    19 ms    19 ms    18 ms  10.88.247.46
11    24 ms    18 ms    18 ms  10.88.8.86
12    21 ms    21 ms    20 ms  10.88.28.54
13    23 ms    21 ms    21 ms  10.88.28.217
14    23 ms    22 ms    23 ms  10.88.28.225
15    26 ms    25 ms    25 ms  10.88.28.110
16    26 ms    89 ms    26 ms  10.88.28.118
17    89 ms    31 ms    26 ms  10.88.28.134  <<< target this, the last 10.88
18    89 ms    89 ms    28 ms  10.22.64.250
19    35 ms    35 ms    38 ms  10.23.251.54
20    35 ms    35 ms    35 ms  192.168.1.1

Objective:

to extract hop that contain the last 10.88 ip address as per above example.

Desire Output:

HOST         HOP
192.168.1.1  10.88.28.134
....

Input will be from text file contain multiple Destination IP Address or direct IP. As below eample

Input.txt

192.168.1.1
192.168.5.31
192.168.65.3

Appreciate anyone can share powershell script for this.

function traceX
{
param ()
....
}

Syntax suggestion:

  • Read from text file >> traceX -f c:\input.txt -x 10.88
  • Direct IP Address >> traceX 192.168.1.1 -x 10.88

Tqvm

1
  • clueless...as of now. Commented Jun 3, 2016 at 0:56

2 Answers 2

1

While your exact requirements are still unclear, the following may get you started; it does NOT address your request for concurrency.
The function is intentionally kept simple; there is potential for better parameter handling using parameter sets and array parameters.

function traceX {
    param(
        [string] $FileOrIp,
        [string] $IpPrefix
    )
    # Determine if an IP address or a filename was passed.
    if ($FileOrIp -match '^\d{1,3}\.\d{1,3}\.\d{1,3}.\d{1,3}$') {
        $ips = $FileOrIp
    } else {
        $ips = Get-Content $FileOrIp
    }
    # Loop over all IPs
    foreach ($ip in $ips) {
        # Get the last matching hop and split the line into fields.
        # Note that the unary form of `-split` performs Awk-style splitting
        # by whitespace, which includes ignoring leading and trailing whitespace.
        $lastMatch = -split ((tracert -d -w 10 $ip) -match " $IpPrefix[^ ]+ $")[-1]
        # Get the last hop's IP.
        if (-not $lastMatch) {
            $matchedIp = 'Not Matched'
        } else {
            $matchedIp = $lastMatch[-1]            
        }
        # Output the result.
        [pscustomobject] [ordered] @{ HOST = $ip; HOP = $matchedIp }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

exactly what i want..tqvm @mklement0. :)
i have 1450 dstip.. it takes me approximately 6 to 7 hours to complete. Hmm..Get-Job cmdlet..reading :(
My pleasure; yes, Start-Job, is worth considering; this answer may help.
0

Deliver what i need, partially.

The function:

function traceX {
    param($f,[string]$x)
    $ips = Get-Content "$f" 
    ForEach ($ip in $ips) {
    $a = "0"
    tracert -d -w 10 $ip | ForEach-Object {
        if ($_.Trim() -match "$x") 
        {
            $n,$a1,$a2,$a3,$node,$null = $_.Trim()-split"\s{2,}"
            $Properties = @{xNode = $node; Host = $ip;}
            $a = "1"              
        }      
    }
    if ($a -match "0")
    {$Properties = @{xNode = "Not Matched"; Host = $ip;} 
    New-Object psobject -Property $Properties 
    }
    else {New-Object psobject -Property $Properties
    }       
  }
}

tracex -f C:\dstip.txt -x 10.88. | Out-File "C:\output.txt"

1 Comment

How to make it to run atleast 50 concurrent session?. tq

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.