Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
206 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...