File: rejects.py

package info (click to toggle)
pymilter 0.8.9-3
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 928 kB
  • ctags: 559
  • sloc: python: 3,605; ansic: 978; sh: 316; makefile: 46
file content (38 lines) | stat: -rw-r--r-- 972 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# Analyze milter log to find abusers

fp = open('/var/log/milter/milter.log','r')
subdict = {}
ipdict = {}
spamcnt = {}
for line in fp:
  a = line.split(None,4)
  if len(a) < 4: continue
  dt,tm,id,op = a[:4]
  if op == 'Subject:':
    if len(a) > 4: subdict[id] = a[4].rstrip()
  elif op == 'connect':
    ipdict[id] = a[4].rstrip()
  elif op in ('eom','dspam'):
    if id in subdict: del subdict[id]
    if id in ipdict: del ipdict[id]
  elif op in ('REJECT:','DSPAM:','SPAM:','abort'):
    if id in subdict:
      if id in ipdict:
        ip = ipdict[id]
	del ipdict[id]
	f,host,raw = ip.split(None,2)
	if host in spamcnt:
	  spamcnt[host] += 1
	else:
	  spamcnt[host] = 1
      else: ip = ''
      print dt,tm,op,a[4].rstrip(),subdict[id]
      del subdict[id]
    else:
      print line.rstrip()
print len(subdict),'leftover entries'

spamlist = filter(lambda x: x[1] > 1,spamcnt.items())
spamlist.sort(lambda x,y: x[1] - y[1])
for ip,cnt in spamlist:
  print cnt,ip