Powershell-自定义Tracert [英] Powershell - Custom Tracert

查看:188
本文介绍了Powershell-自定义Tracert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tracert输出:

 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


目标:

提取包含上述最后一个10.88 ip地址的跃点 例子.

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

需求输出:

HOST         HOP
192.168.1.1  10.88.28.134
....

输入将来自包含多个目标IP地址或直接IP的文本文件.如下示例

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

感谢任何人都可以为此共享Powershell脚本.

Appreciate anyone can share powershell script for this.

function traceX
{
param ()
....
}

语法建议:

  • 从文本文件中读取>> traceX -f c:\ input.txt -x 10.88
  • 直接IP地址>> traceX 192.168.1.1 -x 10.88
  • Read from text file >> traceX -f c:\input.txt -x 10.88
  • Direct IP Address >> traceX 192.168.1.1 -x 10.88

Tqvm

推荐答案

尽管您的确切要求尚不清楚,但以下内容可能会让您入门;它不能满足您的并发请求.
该功能有意保持简单;使用参数集和数组参数可以更好地处理参数.

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

这篇关于Powershell-自定义Tracert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆