2017-12-05 4 views
0

Ich muss die Anzahl der Angriffe pro Stunde pro IP aus einer Protokolldatei mit Python-Skript ausgeben. Ich habe es geschafft, alles fertig zu bekommen, aber ich kann nicht herausfinden, wie ich die Anzahl der Angriffe zu einem endgültigen Gesamtwert hinzufügen kann. Mir fehlt das Wissen, das benötigt wird, um zu verstehen, wie die Gesamtfunktion außerhalb einfacher Berechnungen funktioniert. Vielen Dank im Voraus für jede Hilfe.Wie addieren Sie eine Ausgabe aus einer Protokolldatei mit Python

import re 
from itertools import groupby 
total = 0 

with open(log_file) as Authlog: 
    Authlog = (line for line in Authlog if "Failed password for" in line) 

    for key, group in groupby(Authlog, key = lambda x: x[:9] + re.search('from(.+?) port', x).group(1)): 
     hour, month, day = key[0:3], key[4:6], key[7:9] 
     print ("Within the hour of %s:00 on %s-%s There was %d attacks"%(day, month, hour, len(list(group)))) 
     Total = total +1 

print("Total amount of attacks: %s" % total) 

Was erwarte ich das Ergebnis sein:

Within the hour of 08:00 on 3-Feb There was 172 attacks 
Within the hour of 13:00 on 3-Feb There was 4 attacks 
Within the hour of 21:00 on 3-Feb There was 1 attacks 
Within the hour of 08:00 on 4-Feb There was 15 attacks 
Within the hour of 10:00 on 4-Feb There was 58 attacks 
Within the hour of 10:00 on 4-Feb There was 2 attacks 
Within the hour of 16:00 on 4-Feb There was 4 attacks 
Within the hour of 07:00 on 5-Feb There was 24 attacks 
Within the hour of 08:00 on 5-Feb There was 86 attacks 
Total amount of attacks: 366 

Was ich eigentlich:

Within the hour of 08:00 on 3-Feb There was 172 attacks 
Within the hour of 13:00 on 3-Feb There was 4 attacks 
Within the hour of 21:00 on 3-Feb There was 1 attacks 
Within the hour of 08:00 on 4-Feb There was 15 attacks 
Within the hour of 10:00 on 4-Feb There was 58 attacks 
Within the hour of 10:00 on 4-Feb There was 2 attacks 
Within the hour of 16:00 on 4-Feb There was 4 attacks 
Within the hour of 07:00 on 5-Feb There was 24 attacks 
Within the hour of 08:00 on 5-Feb There was 86 attacks 
Total amount of attacks: 9 
+3

Ändern Sie einfach Ihre 'total = total + 1' zu' total = total + len (Liste (Gruppe)) ' –

+0

Warum' Total = Summe + 1', sollten Sie 'total = total + 1' setzen –

Antwort

0

Variablenname Fall nicht egal. Ändern Sie einfach die Zeile:

Total = total +1 

zu

total = total + len(list(group)) 

weil innerhalb einer Schleife Ihre totals Zähler ist falsch: statt es nur Iterationszahlcode zählt. Und der Wert wird auf eine neue Variable Total in jedem Loop-Schritt erstellt wird, verliert.

+0

Vielen Dank Es hat ein bisschen gefummelt und neu arrangiert, aber das hat es sortiert. Ich habe es am Anfang versucht, aber es kommt immer noch als 0 zurück, aber mit allem an der richtigen Stelle ist es perfekt. – Jamie

Verwandte Themen