Navigation
Artikel
Stuff
RSS Feeds
|
Sourcecodes - Emails in Qmail-Logs verfolgenSprachenübersicht/Python/Email Keywords: qmail log digger digging python tool script Viele Sysadmins kennen die Problematik: ein Kunde beschwert sich über eine nicht angekommene Mail und als Admin muss man die Unschuld beweisen. Da gerade bei belasteten Mail-Servern die Logfülle schnell ansteigt, ist ein kleines Werkzeug sehr hilfreich, um Mails anhand von Absender, Empfänger oder beidem zu verfolgen.
digger.py: #!/usr/bin/env python """ usage: ./digger.py --file /path/to/mail.log[.gz] --from email@domain.tld and|or --to email@domain.tld changelog: v1.0, 29.01.2008: first usable version v1.1, 29.01.2008: added gzip-support v1.2, 30.01.2008: reduced output author: julian golderer <glua (at) 4-mail.net> """ import sys import os def main(): "get the maillog" try: pos = sys.argv.index('--file') logfile = sys.argv.pop(pos + 1) except: print "please specify --file" sys.exit(1) "get the search strings" try: pos = sys.argv.index('--to') to_addr = sys.argv.pop(pos + 1) except: to_addr = '' try: pos = sys.argv.index('--from') from_addr = sys.argv.pop(pos + 1) except: from_addr = '' if not (to_addr or from_addr): print "you have to specify --to or --from at least" sys.exit(1) "open the logfile" try: if logfile.endswith('.gz'): import gzip f = gzip.open(logfile) else: f = file(logfile) except: print "unable to read " + logfile sys.exit(1) "read the file" content = f.readlines() result_lines = {} "if from is defined .." if from_addr: for i in content: if "from <" + from_addr + ">" in i: result_line = content.index(i) msgid = content[result_line].split(' ')[7][:-1] for y in content[result_line:result_line+20]: if (not to_addr and "msg " + msgid + " to local " in y) or (to_addr and "msg " + msgid + " to local " + to_addr in y): deliverid = content[content.index(y)].split(' ')[7][:-1] result_lines[content.index(y)] = {'msgid': msgid, 'deliverid': deliverid} else: "if to is defined .." for i in content: if "to local " + to_addr in i: result_line = content.index(i) msgid = content[result_line].split(' ')[9] deliverid = content[result_line].split(' ')[7][:-1] for y in content[result_line-10:result_line]: if "info msg " + msgid + ": bytes" in y: result_lines[content.index(y)] = {'msgid': msgid, 'deliverid': deliverid} "lets cut the log" for i in result_lines.keys(): msgid = content[i].split(' ')[7][:-1] for line in content[(i-10):(i+20)]: if (result_lines[i]['msgid'] in line or result_lines[i]['deliverid'] in line) and 'qmail:' in line: print content[content.index(line)].strip() print '' f.close() if __name__ == '__main__': main() Gibt es noch irgendwelche Fragen, oder wollen Sie über den Artikel diskutieren? Sprachenübersicht/Python/Email/Emails in Qmail-Logs verfolgen |