-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspamtrap.py
More file actions
31 lines (24 loc) · 891 Bytes
/
spamtrap.py
File metadata and controls
31 lines (24 loc) · 891 Bytes
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
#!/usr/bin/python
import urllib
import re
import sys
import string
import logging
import logging.handlers
from time import strftime
URL="http://blns1.mydomain.com"
# Set up a specific logger with our desired output level
my_logger = logging.getLogger('MyLogger')
my_logger.setLevel(logging.DEBUG)
# Add the log message handler to the logger
handler = logging.handlers.RotatingFileHandler(
'/var/log/spam/spamtrap.log', maxBytes=20000, backupCount=5)
my_logger.addHandler(handler)
line = sys.stdin.read()
# The sort of line we get at the top of a mail:
# Received: from hostname.spammer.com (hostname.spammer.com [111.222.123.123])
header = re.search(r'Received: from .*\[([\d\.]+)\]', line)
if header:
ip = header.group(1)
my_logger.debug("Reporting IP {} as spam, based on {}".format(ip, header.group(0)))
f = urllib.urlopen("{}/report/{}".format(URL, ip))