Finding Gmail SMTP entries in Sendmail log files

I wanted to determine how many connections I was receiving per day from Gmail Simple Mail Transfer Protocol (SMTP) servers to my email server running Sendmail on a CentOS Linux system and the IP addresses of the Gmail servers that were sending email to users on my server. So I created a simple Python script to search for lines in the maillog file, /var/log/maillog for any lines containing "relay" and "" on the same line, since the Gmail servers are in Google's domain.


# Name:
# Created: 2017-10-01
# Last modified; 2017-10-01
# Version: 0.1
# Purpose: search /var/log/maillog for all entries where a Google server
# is the sending SMTP server. Those entries will contain text similar to
# " []"

import os, re, sys

inFile = "/var/log/maillog"

# Check on whether the file exists and is accessible
if not os.path.isfile(inFile):
   print "Error - input file", inFile, "is not accessible!"
   f = open(inFile, "r")

searchStr = "relay=(.*\.google\.com) \[(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\]"

for line in f:
    if "relay=" and "" in line:
       searchObj =, line)
       if searchObj:
          FQDN =
          IP   =
          print FQDN, IP

I import the os module so I can use os.path.isfile(inFile) to ensure that the specifiled log file exists and the sys module so I can exit the program immediately with sys.exit(1) indicating the program was terminated when it encountered an error. I import the re module so that I can use a regular expression for the search on each line of the input file.

The script prints the fully qualified domain name (FQDN) and the IP address for each entry in the log file that references one of Google's Gmail serves. When I ran the script on today's log file, I saw the following output:

# ./

There were 19 entries for the day at the time I ran the script:

# ./ | wc -l