-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·128 lines (97 loc) · 3.32 KB
/
main.py
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3.7
# -*- coding: utf-8 -*-
from subprocess import check_output
import traceback
import sys
import json
import hermes_notify
from datetime import datetime
import socket
from jinja2 import Template
from configparser import ConfigParser
def read_config():
import os
if not os.path.exists('config.ini'):
print("Error: config file is not found!!!", file=sys.stderr)
sys.exit(1)
config = ConfigParser(allow_no_value=True)
config.read('config.ini')
send_email = config['default'].getboolean('send_email')
ip_address = [ip for ip in config['ip_address']]
to_address = []
for address in config['email_to_address']:
to_address.append(address)
email_config = {
'to_address': to_address,
'mail_user': config['email']['user'],
'mail_pwd': config['email']['password'],
'smtp': config['email']['smtp'],
'smtp_port': config['email']['port']
}
return {
'config': config,
'email_config': email_config,
'send_email': send_email
}
def main(all_config):
endResult = []
config = all_config['config']
email_config = all_config['email_config']
send_email = all_config['send_email']
for serverIp in config['ip_address']:
try:
rt = checkIp(serverIp, all_config)
if rt is not None:
endResult.append(rt)
except Exception as err:
print(traceback.format_exc())
endResult.append({"ip": serverIp,
"status": "error"})
hasBlacklisted=False
for result in endResult:
if result["status"]=="listed":
hasBlacklisted=True
notifyObj = hermes_notify.HermesNotify(**email_config)
if hasBlacklisted:
notify(endResult, notifyObj, email_config)
else:
sendRelief(notifyObj, send_email)
def notify(result, notifyObj, send_email = False):
template = Template(open('layout.html', 'r').read())
rst = template.render(results=result)
if send_email:
notifyObj.warn_html(datetime.now().strftime("%Y-%m-%d %H:%M:%S")+" MAIL SERVER IPS CHECK", rst)
else:
print(rst)
def sendRelief(notifyObj, send_email = False):
if send_email:
notifyObj.warn(datetime.now().strftime("%Y-%m-%d %H:%M:%S")+" MAIL SERVER IPS CHECK", "HOORAY!! NONE OF THE IPs LISTED!!!")
else:
print("HOORAY!! NONE OF THE IPs LISTED!!!")
def checkIp(serverIp, all_config):
config = all_config['config']
reverseIp=getReversedIp(serverIp)
markedLists=[]
for dnsbl in config['blacklists']:
try:
# print("dig +short {}.{}".format(reverseIp, dnsbl))
answer = check_output(["dig" ,"+short", reverseIp + "." + dnsbl])
answer=answer.decode().strip()
if answer.startswith('127'):
markedLists.append({"answer":answer,"bl":dnsbl})
except:
pass
if len(markedLists)!=0:
try:
ptr = socket.gethostbyaddr(serverIp)[0]
except:
ptr = 'No Reverse'
return {"ip":serverIp,
"status":"listed",
"markedLists":markedLists,
"ptr": ptr}
def getReversedIp(serverIp):
return ".".join(serverIp.split(".")[::-1])
if __name__ == '__main__':
config = read_config()
main(config)