#!/usr/bin/python from optparse import OptionParser parser = OptionParser() parser.add_option('-d', '--debug', action="store_true", dest="debug") parser.add_option('-v', '--verbose', action="store_true", dest="verbose") (options, args) = parser.parse_args() import logging if options.debug: loglevel=logging.DEBUG elif options.verbose: loglevel=logging.INFO else: loglevel=logging.WARNING logging.basicConfig(level=loglevel,format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p') import subprocess import re mailq_p=subprocess.Popen(['mailq'], stdout=subprocess.PIPE) mailq=mailq_p.communicate()[0] senders={} current_sender=None for line in mailq.split('\n'): m=re.match('^([A-Z0-9]+)\*?[ \t].* ([^ ]+)$',line) if m: sender=m.group(2) logging.debug("New mailid of %s" % sender) if sender not in senders: senders[sender]={'count': 0, 'mailid': []} senders[sender]['mailid'].append(m.group(1)) current_sender=sender continue m=re.match('^ *([^ ]+@.+)$',line) if m: if current_sender is None: print "??? : %s" % line continue logging.debug("New mail for %s" % current_sender) senders[current_sender]['count']+=1 top_senders=senders.keys() top_senders=sorted(top_senders, key=lambda sender: senders[sender]['count'],reverse=True) for s in top_senders: print "%s\t%s" % (senders[s]['count'],s)