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地址的跃点 示例
欲望输出:
HOST HOP
192.168.1.1 10.88.28.134
....
输入将来自文本文件,包含多个目标IP地址或直接IP。如下例所示
INPUT.TXT
192.168.1.1
192.168.5.31
192.168.65.3
感谢任何人都可以为此分享powershell脚本。
function traceX
{
param ()
....
}
语法建议:
Tqvm
答案 0 :(得分:1)
虽然您的具体要求仍不明确,但以下内容可能会让您开始;它没有解决您的并发请求 该功能有意保持简单;使用参数集和数组参数可以更好地进行参数处理。
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 }
}
}
答案 1 :(得分:0)
部分地提供我需要的东西。
功能:
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”