我有多个带有某种规则库的 CSV,我需要从中解析和创建命令,但我遇到了多个问题。
在开始之前,这里有一个布局示例,以及它的外观:
$1 = Rule number
$3 = Source
$4 = Destination
$5 = Service
$6 = Action
$7 = Track
$10 = Comments
.
Security Policy: Blahblahblah,,,,,,,,,
12,,host_A,net-B,https,drop,Log,Any,Any,comments
13,,host_A,net-B,smtp,drop,Log,Any,Any,comments
14,,host_A,net-B,http,accept,Log,Any,Any,comments
,,net-C,,,,,,,
,,net-D,,,,,,,
15,,host_A,net-B,http,accept,Log,Any,Any,comments
,,host_B,net-C,service_X,,,,,
,,host_C,net-D,service_y,,,,,
,,host_D,,,,,,,
,,host_E,,,,,,,
问题 1:需要在循环内调整第 1 列(规则编号)。我需要从中减去一个变量以等于正确的数字(需要移位)。例如,第一条规则#12,需要在循环中成为#1。
我用它来创建我需要从每个连续行的原始变量中减去的变量(取第一行,减去一个):
`awk -F, 'NR==2 {print $1 -1 }'
问题 #2:我需要在 Rule# 的每个实例上迭代这个循环。IE:每个规则“可以”有多个来源/目的地/服务,我需要能够将新对象与正确的规则联系起来。
还需要检查 $1 的错误,因为有一些字段/规则需要跳过,它们以“禁用”或类似的东西开头。这似乎可以解决问题:
awk -F, '$1 ~ "^[0-9]*$" {print $1}
总的来说,我希望最终输出如下所示:
(所有 echo'd/awk print'd 等):
if new rule # is found in $1:
create rule security_rule
create action $rule_number $action
create comment $rule_number $comment
create source $rule_number $source <--- iterate as many times as required
create destination $rule_number $destination <--- iterate as many times as required
create service $rule_number $service <--- iterate as many times as required
create track $rule_number $track
ETC...
您可以提供的任何帮助/建议将不胜感激。
谢谢,
编辑:一个更好的例子(规则 1 = CSV 中的规则 12 - 这些仍然是粗略的打印语句,我可以稍后填写正确的打印值):
if new rule # is found in $1:
create rule security_rule
create action rule 1 drop
create comment rule 1 "This is a comment"
create source rule 1 host_A
create destination rule 1 net-B
create service rule 1 https
create track rule 1 Log
具有多个源/目标/服务的那些将简单地添加额外的“创建源规则 x”行,如下所示:
if new rule # is found in $1:
create rule security_rule
create action rule 3 accept
create comment rule 3 "This is a comment"
create source rule 3 host_A
create source rule 3 net-C
create source rule 3 net-D
create destination rule 3 net-B
create service rule 3 http
create track rule 3 Log