import
sys,os,nmap
import
multiprocessing
import
httplib,smtplib
from
email.MIMEText
import
MIMEText
from
email.Header
import
Header
reload
(sys)
sys.setdefaultencoding(
'utf8'
)
mailto_list
=
[
'zhaodong@tshare365.tv'
]
mail_host
=
"smtp.163.com"
mail_user
=
"tshare365@163.com"
mail_pass
=
"www.tshare365.com"
mail_postfix
=
"163.com"
def
send_mail(to_list,sub,content):
me
=
"服务器端口异常报警"
+
"<"
+
mail_user
+
"@"
+
mail_postfix
+
">"
msg
=
MIMEText(content,_subtype
=
'plain'
,_charset
=
'utf_8'
)
msg[
'Subject'
]
=
sub
msg[
'From'
]
=
me
msg[
'To'
]
=
";"
.join(to_list)
try
:
server.login(mail_user,mail_pass)
server.sendmail(me, to_list, msg.as_string())
server.close()
return
True
except
Exception, e:
print
str
(e)
return
False
def
HostCheck(ipaddr):
nm
=
nmap.PortScanner()
call
=
nm.scan(ipaddr,
'22-65535'
)
nm.command_line()
nm.scaninfo()
nm.all_hosts()
for
host
in
nm.all_hosts():
for
proto
in
nm[host].all_protocols():
pass
lport
=
nm[host][proto].keys()
lport.sort()
for
port
in
lport:
if
port
in
White_PORT:
print
line
else
:
line
=
"HOST: %s\tTCP/UDP: %s\tPORT : %s\t 异常端口"
%
(host, proto, port)
f
=
file
(
'/tmp/Problem_info.txt'
,
'a'
)
f.write(
"\r\n"
)
f.write(line)
f.close()
if
__name__
=
=
"__main__"
:
INPUT_IP
=
os.path.join(os.getcwd(),
"IP.txt"
)
INPUT_IP_LINES
=
sum
(
1
for
line
in
open
(INPUT_IP))
OPEN_INPUT_IP
=
open
(INPUT_IP)
if
INPUT_IP_LINES >
30
:
process_number
=
30
else
:
process_number
=
INPUT_IP_LINES
White_PORT
=
[
22
,
80
,
3306
]
pool
=
multiprocessing.Pool(processes
=
process_number)
for
IP
in
OPEN_INPUT_IP.readlines():
IP
=
IP.strip(
'\n'
)
pool.apply_async(HostCheck,(IP,))
pool.close()
pool.join()
if
os.path.exists(
"/tmp/Problem_info.txt"
):
infor
=
os.popen(
"cat /tmp/Problem_info.txt"
).read()
send_mail(mailto_list,
"zhaodong"
,infor)
os.system(
"rm -rf /tmp/Problem_info.txt"
)
网友评论