gpt4 book ai didi

powershell - 如何使用Powershell遍历CSV行和导出表

转载 作者:行者123 更新时间:2023-12-03 00:42:32 24 4
gpt4 key购买 nike

我对Powershell有点陌生,但是在我的新工作中,我发现它有很多用途,目前,它是我可以访问的唯一脚本语言之一。
我正在尝试审核一些防火墙流量报告,这些报告以PDF和CSV格式提供。报告的组织方式使这一点有些棘手(至少对我而言)。
它仅显示为两列,第二列为 session 计数。第一列是棘手的部分。它包含目标地址本身,目标端口(由带有标题的几个选项卡缩进)以及每个目标端口计数的源IP(由标题加上多个缩进)。

我提供的摘录只是一个小样本,我已经更改了供公众使用的信息。最终,我希望能够专注于目标端口,并了解有关该端口上最常使用的目标地址以及哪些源IP的更多信息。
最终目标是能够审核我们许多现有的似乎过于宽松的防火墙规则,并开始在其之上制定新规则以“剥离”某些流量。
因为我将不得不做很多事情,所以我认为编写这些报告的分析脚本将有助于简化我自己以及以后任何人的流程。

192.168.250.12,46420
ip.dstport 135, 32783
ip.src 10.10.248.111, 32761
ip.src 172.16.152.96, 22
ip.dstport 23, 13597
ip.src 10.10.248.111, 13590
ip.src 172.16.152.96, 7
ip.dstport 445, 32
ip.src 172.16.152.96, 26
ip.src 10.10.248.111, 6
ip.dstport 22, 8
ip.src 172.16.152.96, 8
192.168.250.14,44788
ip.dstport 135, 31213
ip.src 10.10.248.111, 31182
ip.src 172.16.152.96, 31
ip.dstport 23, 12991
ip.src 10.10.248.111, 12984
ip.src 172.16.152.96, 7
ip.dstport 80, 377
ip.src 10.10.53.133, 215
ip.src 10.10.139.42, 83
ip.src 10.10.53.109, 35
ip.src 10.10.15.196, 32
ip.src 10.10.85.155, 6
ip.src 10.10.15.123, 4
ip.src 10.10.15.148, 2
ip.dstport 445, 146
ip.src 10.10.15.123, 87
ip.src 172.16.152.96, 30
ip.src 10.10.185.151, 8
ip.src 10.10.3.51, 6
ip.src 10.10.248.111, 5
ip.src 10.10.15.19, 4
ip.src 10.10.15.103, 2
ip.src 10.10.248.175, 2
ip.src 10.10.12.37, 1
ip.src 10.10.91.101, 1
ip.dstport 1433, 20
ip.src 10.10.248.111, 20
ip.dstport 3389, 15
ip.src 10.10.51.122, 8
ip.src 10.10.51.126, 4
ip.src 10.10.15.123, 3
ip.dstport 53, 13
ip.src 172.16.152.96, 13
ip.dstport 8443, 9
ip.src 10.10.91.101, 9
ip.dstport 8080, 4
ip.src 172.16.152.96, 4


在Excel中手动移动内容之后,我得到了一个允许使用此功能的表。但是自动化这将是目标
ip.dst  ip.dstport  ip.src  Session_count
192.168.250.12 135 10.10.248.111 32761
192.168.250.12 135 172.16.152.96 22
192.168.250.12 8080 10.10.248.111 13590
192.168.250.12 8080 172.16.152.96 7
192.168.250.12 445 172.16.152.96 26
192.168.250.12 445 10.10.248.111 6
192.168.250.12 8443 172.16.152.96 8
192.168.250.14 135 10.10.248.111 31182
192.168.250.14 135 172.16.152.96 31
192.168.250.14 8443 10.10.248.111 5
192.168.250.14 8443 172.16.152.96 3
192.168.250.14 8080 10.10.248.111 12984
192.168.250.14 8080 172.16.152.96 7
192.168.250.14 80 10.10.53.133 215
192.168.250.14 80 10.10.139.42 83
192.168.250.14 80 10.10.53.109 35

最佳答案

您可以解析这种类型的格式,并通过ForEach-ObjectGet-Content上的简单循环轻松地逐行轻松地创建有意义的输出。

我个人将以switch模式去寻找-regex:

function Import-FireWallLog
{
param(
[Parameter(Mandatory, ValueFromPipelineByPropertyName)]
[Alias('PSPath')]
[string[]]$LiteralPath
)

process {
Write-Host $LiteralPath -ForegroundColor Cyan
switch -regex -file ($LiteralPath) {
'^(\d{1,3}(?:\.\d{1,3}){3}),' {
# Grab the capture block value (the dst ip), save for later
$dstIP = $Matches[1]
continue
}

'ip\.dstport\s+(\d+),' {
# Grab the capture block value (the dst port), save for later
$dstPort = $Matches[1]
continue
}

'ip\.src\s+(\d{1,3}(?:\.\d{1,3}){3}),\s+(\d+)' {
# Emit a new object with the ip.src, session_count and previously stored dst info
[pscustomobject]@{
'ip.dst' = $dstIP
'ip.dstport' = $dstPort
'ip.src' = $Matches[1]
'session_count' = $Matches[2]
}
continue
}

default {
# a line format we don't recognize
# let's write it to the Debug stream
Write-Debug -Message "Encountered unrecognized input: '$_'"
}
}
}
}

关于powershell - 如何使用Powershell遍历CSV行和导出表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57500376/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com