26
26
logger = logging .getLogger (__name__ )
27
27
28
28
29
+ def printv (message , * args , ** kwargs ):
30
+ if args .verbose == True :
31
+ print (message .format (* args , ** kwargs ))
32
+
33
+
29
34
@functools .lru_cache (maxsize = None )
30
35
def resolve_hostname (ip ):
31
36
return socket .getfqdn (ip )
@@ -160,6 +165,8 @@ def total_packets(self):
160
165
help = "The file to analyze (defaults to stdin if not provided)" )
161
166
parser .add_argument ('-p' , '--packets' , dest = 'packets_threshold' , type = int , default = 10 ,
162
167
help = "Number of packets representing the lower bound in connections to be processed" )
168
+ parser .add_argument ('-v' , '--verbose' , dest = "verbose" , action = "store_true" ,
169
+ help = "Enable verbose output." )
163
170
args = parser .parse_args ()
164
171
165
172
# Using a file and using stdin differ in their further usage for gzip.open
@@ -198,6 +205,8 @@ def total_packets(self):
198
205
skipped = 0
199
206
skipped_threshold = args .packets_threshold
200
207
208
+ first_line = True # print header line before first line
209
+
201
210
for key in sorted (data ):
202
211
timestamp = datetime .fromtimestamp (float (key )).strftime ("%Y-%m-%d %H:%M.%S" )
203
212
client = data [key ]["client" ]
@@ -236,13 +245,22 @@ def total_packets(self):
236
245
skipped += 1
237
246
continue
238
247
239
- print ("{timestamp}: {service:<14} | {size:8} | {duration:9} | {packets:5} | Between {src_host} ({src}) and {dest_host} ({dest})" \
248
+ if first_line :
249
+ print ("{:19} | {:14} | {:8} | {:9} | {:7} | Involved hosts" .format ("Timestamp" , "Service" , "Size" , "Duration" , "Packets" ))
250
+ print ("-" * 100 )
251
+ first_line = False
252
+
253
+ print ("{timestamp} | {service:<14} | {size:8} | {duration:9} | {packets:7} | Between {src_host} ({src}) and {dest_host} ({dest})" \
240
254
.format (timestamp = timestamp , service = con .service .upper (), src_host = con .hostnames .src , src = con .src ,
241
255
dest_host = con .hostnames .dest , dest = con .dest , size = con .human_size , duration = con .human_duration ,
242
256
packets = con .total_packets ))
243
257
244
258
if skipped > 0 :
245
- print (f"{ skipped } connections skipped, because they had less than { skipped_threshold } packets." )
259
+ print (f"{ skipped } connections skipped, because they had less than { skipped_threshold } packets (this value can be set with the -p flag)." )
260
+
261
+ if not args .verbose :
262
+ # Exit here if no debugging session was wanted
263
+ exit (0 )
246
264
247
265
if len (pending ) > 0 :
248
266
print (f"There are { len (pending )} first_switched entries left in the pending dict!" )
@@ -260,4 +278,4 @@ def total_packets(self):
260
278
print (first_switched , peer , flow ["IPV6_DST_ADDR" ], flow ["IN_PKTS" ])
261
279
262
280
if all_noise :
263
- print ("They were all noise!" )
281
+ print ("They were all noise!" )
0 commit comments