Question: I have this code and i am trying to fix it so that the output of the time will be in the format YYYY
I have this code and i am trying to fix it so that the output of the time will be in the format YYYYMMDD HH:MM:SSpm HHMM which includes the timezone offset.
here" is my code
loganalyzer.py
import re
from datetime import datetime
import csv
class LogEntry:
def initself eventtime, internalip portnumber, protocol, action, ruleid sourceip country, countryname:
self.eventtime datetime.strptimeeventtime, Ymd H:M:S Z
self.internalip internalip
self.portnumber portnumber
self.protocol protocol
self.action action
self.ruleid ruleid
self.sourceip sourceip
self.country country
self.countryname countryname
@property
def ipvclassself:
firstoctet intselfsourceipsplit
if firstoctet :
return A
elif firstoctet :
return B
elif firstoctet :
return C
elif firstoctet :
return D
else:
return 'Unknown'
testloganalyzer.py
import unittest
from loganalyzer import LogEntry
class TestLogEntryunittestTestCase:
def testeventtimeconversionself:
logentry LogEntryeventtime :: UTC", internalip portnumber protocol TCP
action "ALLOW", ruleid sourceip country US countryname "United States"
self.assertEquallogentry.eventtime.month,
self.assertEquallogentry.eventtime.hour,
def testipvclassself:
logentrya LogEntry:: UTC", TCP "ALLOW", US "United States"
logentryb LogEntry:: UTC", TCP "ALLOW", US "United States"
logentryc LogEntry:: UTC", TCP "ALLOW", US "United States"
logentryd LogEntry:: UTC", TCP "ALLOW", US "United States"
self.assertEquallogentryaipvclass, A
self.assertEquallogentrybipvclass, B
self.assertEquallogentrycipvclass, C
self.assertEquallogentrydipvclass, D
if namemain:
unittest.main
index.py
from loganalyzer import LogEntry
import argparse
import csv
import pytz
def parseargs:
parser argparse.ArgumentParserdescription"Analyze firewall log CSV files."
parser.addargumentfilename", typestr help"The filename of the CSV file containing the log data."
return parser.parseargs
def main:
args parseargs
filename args.filename
logentries
with openfilename newline as csvfile:
reader csvDictReadercsvfile
for row in reader:
logentry LogEntryroweventtime' rowinternalip rowportnumber' rowprotocol rowaction rowruleid rowsourceip rowcountry rowcountryname'
logentries.appendlogentry
for logentry in logentries::
printflogentry.eventtime.strftimeYmd H:M:Slogentry.actionlogentry.sourceiplogentry.ipvclasslogentry.countryname
if namemain:
main
below is the csv file and the wrong output in the terminal
firewalllogssample.cSv data
eventtime,internalip portnumber, protocol, action, ruleid ruledescription, asdomain, asname, asn, continent, continentname, country,
:: UTC,FTP Data, Allow, Allow traffic for mobile device management MDM services,greenhouse!
:: UTC,SMTP Simple Mail Transfer Protocol, Deny,Block traffic for known buffer overflow comm
:: UTC,IMAPS Encrypted IMAP, Allow, Allow traffic for remote desktop support tools, hostiservel
:: UTC,NetBIOS Name Service, Deny, Block traffic for known XSS payloads,
cloudflare.com, "cloudfla
:: UTC, IMAP Internet Message Access Protocol,Allow,Allow traffic for certificate revocation
:: UTC,FTP Control, Allow, Allow traffic for network printer discovery,
cybera.ca Cybera Inc, A:
:: UTC,OpenVPN, Log, Log LDAP traffic for directory services, gtt net, GTT Communications Neth
:: UTC,LDAPS Encrypted LDAP, Warning, warn on repeated RDP connection attempts to specific
:: UTC,IMAP Internet Message Access Protocol,Allow,Allow traffic for network backup and re
