使用 PowerShell 从 CSV 文件中提取数据

Extracting Data from CSV Files Using PowerShell

我有一个 syslog 服务器将日志从我们的防火墙设备转储到 CSV 文件。我正在使用那个 csv 文件,并想在日志中提取某些数据,例如源和目标 IP 地址,以便我可以 运行 稍后对它们进行进一步分析。

这是源文件中单个记录的示例。请注意数据中包含的 IP 地址如何使用 "src_ip"、"dst_ip"、"tran_src_ip" 或 "tran_dest_ip" 作为前缀。 注意:我已经编辑了 IP 地址以屏蔽它们。

2018-07-01 14:48:47,Local7.Info,192.168.1.00,device="SFW" date=2018-07-01 time=14:48:39 timezone="PDT" device_name="XG" device_id=00000000000000 log_id=010101600001 log_type="Firewall" log_component="Firewall Rule" log_subtype="Allowed" status="Allow" priority=Information duration=11 fw_rule_id=3 policy_type=3 user_name="" user_gp="" iap=0 ips_policy_id=0 appfilter_policy_id=0 application="Secure Socket Layer Protocol" application_risk=1 application_technology="Network Protocol" application_category="Infrastructure" in_interface="Port2" out_interface="Port1" src_mac=00: 0:00: 0:00: 0 src_ip=75.148.000.000 src_country_code=USA dst_ip=23.24.000.000 dst_country_code=USA protocol="TCP" src_port=55000 dst_port=443 sent_pkts=7 recv_pkts=6 sent_bytes=1369 recv_bytes=918 tran_src_ip=192.168.000.000 tran_src_port=0 tran_dst_ip=192.168.000.000 tran_dst_port=0 srczonetype="WAN" srczone="WAN" dstzonetype="LOCAL" dstzone="LOCAL" dir_disp="" connevent="Stop" connid="1782869248" vconnid="" hb_health="No Heartbeat" message="" appresolvedby="Signature"

我已经能够编写一个可以从整个 CSV 文件中提取 IP 地址的脚本,但它没有指定它是 src_ip 还是 dst_ip 等。我希望能够创建一个可以从 CSV 文件中获取数据的脚本,然后创建一个新的 CSV 文件,其中的列包含 src_ip、dest_ip 等

我的代码如下:

$input_path = ‘c:\powershell_work\data.csv’
$output_file = ‘c:\powershell_work\output-file.csv’
$regex = ‘\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b’

$ipaddresses = select-string -Path $input_path -Pattern $regex -AllMatches | 
% { $_.Matches } | % { $_.Value } | out-file $output_file -append
$regex = '\b(\w+)=(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b)'

Select-String -LiteralPath $input_path -AllMatches -Pattern $regex | ForEach-Object {
    $obj = New-Object pscustomobject
    foreach ($match in $_.Matches) {
      Add-Member -InputObject $obj -NotePropertyName $match.Groups[1].Value -NotePropertyValue $match.Groups[2].Value
    }
    $obj
} | Export-Csv -NoTypeInformation $output_file

LotPings 编辑

以上更新输入的示例输出(在 Export-Csv 调用之前):

src_ip         dst_ip        tran_src_ip     tran_dst_ip
------         ------        -----------     -----------
75.148.000.000 23.24.000.000 192.168.000.000 192.168.000.000

通过name提取一组可指定的属性:

# Use a regex that matches all key-value pairs.
$regex = '\b(\w+)=([^ ]+)'

Select-String -LiteralPath $input_path -AllMatches -Pattern $regex | ForEach-Object {
    $obj = New-Object pscustomobject
    foreach ($match in $_.Matches) {
      Add-Member -InputObject $obj -NotePropertyName $match.Groups[1].Value -NotePropertyValue $match.Groups[2].Value
    }
    $obj
} | Select-Object *_ip, srczone, src_country_code, dstzone, dst_country_code | 
     Export-Csv -NoTypeInformation $output_file

请注意,这首先会创建一个具有 所有 输入属性的对象,然后 然后 通过 Select-Object 仅选择感兴趣的对象,这有点低效,但使命令在概念上保持简单,并允许您轻松确定提取 order.