Trying to create a python script that finds all the servers that sent email and output the results in a .csv file, with one column being the server name and the other column being the server IP address, with no repeats in the server name or IP address in the csv file table. The script also needs to use regular expressions to find the the servers that sent an email.
Here is a portion of the log file:
Apr 7 10:25:45 sys postfix/smtpd[667]: disconnect from airwave.nsd.org[10.1.20.13] ehlo=1 mail=1 rcpt=1 data=1 quit=1 commands=5
Here is my attempt at a script to find the server name and IP:
import re
import csv
logscan = re.compile(r'(?<=from)([^\[]+)\[([\d.]+)\](?=\n.*?mail=[1-9][0-9]*)', re.M)
with open('mail.log', 'r') as fin:
for line in fin:
line = line.rstrip()
m = logscan.search(line)
csv_file.writerow([m.group(1).strip(), m.group(2).strip()])
# Run main() if script called directly
if __name__ == "__main__":
main()
When I run this, I get an error saying the csv_file is not defined.