-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfw_stat_ip_list_all_dst.sh
134 lines (112 loc) · 4.9 KB
/
fw_stat_ip_list_all_dst.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/bin/bash
date
# Firewall names to check
#FIREWALLS="firewall01 firewall02"
FIREWALLS="."
# Additional strng to look for
#NEEDLE1=255.255.255.255
# There must be at least two arguments:
# 1- file with a list of IP-addresses to search for
# 2- CheckPoint log file converted to text
if (( $# < 2 )); then
echo "Usage: $0 ip_list_file cp_firewall_log-1.txt cp_firewall_log-2.txt ..."
exit 1
fi
# Output file name
OUTFILE="fwstat_$1_`date +'%Y%m%d_%H%M'`.log"
# The format of the CheckPoint log file must conform the following format
# 2 - date
# 3 - time
# 4 - src
# 5 - dst
# 6 - proto
# 7 - service
# 8 - action
HEADER="num;date;time;src;dst;proto;service;action"
# Convert the list of IP addresses from the file in the 1st argument
# to the following format:
# 1\.2\.3\.4|7\.6\.5\.4
# Replace newlines, spaces and tabs with pipes
# Escape dots with backslashes
iplist=`cat $1 | tr -s '\n\t ' '|' | sed -e 's/|$//' -e 's/^|//' -e 's/\./\\\./g'`
echo $iplist
# Convert the list of IP addresses from the file in the 1st argument
# from ;1.2.3.[0-9]; format to
# ^1\\.2\\.3\\.[0-9]$
# Double back slash is needed for AWK and additional double back slash for shell variable
relist=`cat $1 | sed -e 's/^;/^/' -e 's/;$/$/' | tr -s '\n\t ' '|' | sed -e 's/|$//' -e 's/^|//' -e 's/\./\\\\\\\\./g'`
echo $relist
shift
echo "Being saved to $OUTFILE"
files=""
for file in $*
do
if ( ! zcat $file | head -1 | fgrep $HEADER > /dev/null 2>&1 ); then
echo "$file - wrong file format"
continue
else
echo "$file - good file"
files=$files" "$file
fi
done
echo $files
for fw in $FIREWALLS
do
echo "==================== $fw ==========================="
zcat $files | egrep -v "drop|reject|control" | fgrep -v ";udp;123;" | fgrep -v icmp | egrep "$iplist" | awk -F\; '
# TMPFILE - temporary file will be overwritten
# min - the threshhold for amount of connectins to be shown in the report
BEGIN { min=0; TMPFILE="/tmp/fw_stat_ip_list_top_tmp.tmp";system("echo \ >" TMPFILE);}
{
# Counting either sources (4) or destinations (5)
if ( $5 ~ "'$relist'" ) {
src[$4]++;
service=$6 "-" $7;
srv[service]++;
ipservice=$5 ":" service;
pair[ipservice]++;
connection=$4 " -> " $5 ":" service;
conn[connection]++
}
}
END {
print "==================== sources ==========================="
for ( i in src) {
if (src[i] >= min) print src[i],i >> TMPFILE;
}
fflush();
# Only the top 100. Change the "head" argument if needed
# system("sort -rn " TMPFILE "| head -100");
system("sort -rn " TMPFILE);
system("echo \ >" TMPFILE);
print "==================== services ==========================="
for ( i in srv) {
print srv[i],i >> TMPFILE;
}
fflush();
# Only the top 100. Change the "head" argument if needed
# system("sort -rn " TMPFILE "| head -100");
system("sort -rn " TMPFILE );
system("echo \ >" TMPFILE);
print "==================== destination-services ==========================="
for ( i in pair) {
if (pair[i] >= min) print pair[i],i >> TMPFILE;
}
fflush();
# Only the top 100. Change the "head" argument if needed
# system("sort -rn " TMPFILE "| head -100");
system("sort -rn " TMPFILE);
system("echo \ >" TMPFILE);
print "==================== sources-destination-services ==========================="
for ( i in conn) {
if (conn[i] >= min) print conn[i],i >> TMPFILE;
}
fflush();
# Only the top 100. Change the "head" argument if needed
# system("sort -rn " TMPFILE "| head -100");
system("sort -rn " TMPFILE);
system("echo \ >" TMPFILE);
}'
done > $OUTFILE
echo "Saved to $OUTFILE"
date