我的目标是找到与源IP和目标IP匹配的规则编号
示例:
Source.IP -- 10.44.68.252
Destination -- 10.112.140.155
使用grep "10.44.68.252\|10.112.140.155"
过滤配置文件为我提供了以下命令列表。
我需要手动查找并找到源和目标匹配的规则号。在下面的输出规则1515中,匹配项是我要寻找的。
有什么办法可以在同一命令中找到它吗?
cat test.txt | grep "10.44.68.252\|10.112.140.155"
set nat source rule 39 source address '10.112.140.155/32'
set nat source rule 1008 source address '10.112.140.155/32'
set nat source rule 1010 source address '10.112.140.155/32'
set nat source rule 1036 destination address '10.44.68.252/32'
set nat source rule 1037 destination address '10.44.68.252/32'
set nat source rule 1099 source address '10.112.140.155/32'
set nat source rule 1104 source address '10.112.140.155/32'
set nat source rule 1515 destination address '10.44.68.252/32'
set nat source rule 1515 source address '10.112.140.155/32'
set nat source rule 1516 destination address '10.44.68.252/32'
set nat source rule 1517 source address '10.112.140.155/32'
set nat source rule 1520 source address '10.112.140.155/32'
尝试了grep中的多个选项。没有人帮助。 sed可能会有所帮助。但是我不知道sed。
答案 0 :(得分:2)
awk
解决方案:
awk -v sq="'" -v dest=10.44.68.252 -v src=10.112.140.155 '
(index($NF,sq dest "/") && $6 == "destination") ||
(index($NF,sq src "/") && $6 == "source"){found[$5]++}
END{
for(key in found)
if(found[key]>1)
print key;
}
' filename
输出
1515
注意::我宁愿将其作为注释,但要注意其长度。另请参阅由我实现的@ ed-morton建议的revision
答案 1 :(得分:0)
有些bashisms,但是应该这样做:
sort -uk5 data.txt>&dat && join -j5 <(fgrep e\ ad<dat) <(fgrep n\ ad<dat) | awk '{gsub(/'"'"'/,"");print$1,$8,$NF}' | column -tR1,2,3 -N rule,src,dest
rule src dest
1515 10.112.140.155/32 10.44.68.252/32
答案 2 :(得分:0)
尝试一下:
cat test.txt | grep "10.44.68.252\|10.112.140.155" | awk '{ print $5 }' | uniq -d
输出
1515
答案 3 :(得分:0)
这是AWK解决方案:
awk -v sq="'" -v src=10.112.140.155 -v dst=10.44.68.252 '
BEGIN {
s_regex = sq src "/"; d_regex = sq dst "/"
}
/source address/ && $8 ~ s_regex {s[si] = $5; si++}
/destination address/ && $8 ~ d_regex {d[di] = $5; di++}
END {
for (i in s) for (j in d) if (s[i] == d[j]) {print s[i]}
}
' FILENAME
说明: