0

I'm new to Python, so please bear with me, and apologies in advance if this topic has been covered :)

I'm in the process of writing a script to check a list of SMTP logs from a certain domain. I want to ignore any lines that contain other parts of the SMTP conversation such as DATA or RCPT etc...

Here is the script:

import os
files = os.listdir('t:\smtpsvc1')
path = "t:\smtpsvc1" # I have used this variable as os.path.join wouldn't accept the variable 'files'
for f in files:
    fullpath = os.path.join(path,f)
    print(fullpath)
    searchfile = open(fullpath, "r")
    for line in searchfile:
            if ('FROM' and '@somedomain.local' in line):
                print (line)
searchfile.close()

The script runs, prints the name of the first file then checks the lines and outputs data like this:

2016-09-28 09:31:50 192.168.106.28 APPSERVER SMTPSVC1 SMTPSERVER 192.168.106.124 0 DATA - + 250 0 139 612 47 SMTP - - - -

This seems to trigger the condition looking for @somedomain.local, but seems to be ignoring the FROM part of the if statement. What am I doing wrong? Cheers in advance

Anth
  • 3
  • 2

1 Answers1

1

Change this:

if 'FROM' and '@somedomain.local' in line:

To this:

if 'FROM' in line or '@somedomain.local' in line:

I'm assuming that you want to look for lines which contain either one of them.

If you're actually looking for lines which contain both of them, then change or to and.

barak manos
  • 29,648
  • 10
  • 62
  • 114
  • Thank you very much for your help! – Anth Jan 11 '17 at 10:11
  • @Anth: You're welcome. Feel free to accept the answer by clicking on the V next to it :) – barak manos Jan 11 '17 at 10:28
  • Hmm changed to your suggestion, and got a lot of lines containing the following (or very similiar) 2016-09-25 07:18:53 74.125.71.28 OutboundConnectionCommand SMTPSVC1 APPSERVER - 25 MAIL - FROM:+SIZE=2389 0 0 4 0 234 SMTP - - - - So now its evaluating the line and outputing lines with FROM in, but ignoring the somedomain.local part? Weird?! – Anth Jan 11 '17 at 10:29
  • @Anth: Did you read what I wrote at the bottom line??? – barak manos Jan 11 '17 at 10:35
  • Ah that worked! Thank you very much! I'll mark this as answered :) – Anth Jan 11 '17 at 11:06