Powershell - Custom Tracert

安稳与你 提交于 2019-12-26 05:38:08

问题


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:


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



回答2:


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"



来源:https://stackoverflow.com/questions/37603190/powershell-custom-tracert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!