python-nmap 数据库_Python-Nmap使用及案例

# !/usr/bin/python

# -*- coding:utf-8 -*-

import nmap

import re

import mytools as tool

import sys

from multiprocessing import Pool

from functools import partial

reload(sys)

sys.setdefaultencoding('utf8')

def nmScan(host, portrange, whitelist):

p = re.compile("^(\d*)\-(\d*)$")

# if type(hostlist) != list:

#    help()

portmatch = re.match(p, portrange)

if not portmatch:

help()

if host == '121.42.32.172':

whitelist = [25, ]

result = ''

nm = nmap.PortScanner()

tmp = nm.scan(host, portrange)

result = result + "

ip地址:%s 主机名:[%s]  ......  %s


" % (

host, tmp['scan'][host]['hostname'], tmp['scan'][host]['status']['state'])

try:

ports = tmp['scan'][host]['tcp'].keys()

for port in ports:

info = ''

if port not in whitelist:

info = 'Alert:非预期端口  '

else:

info = 'Info:正常开放端口  '

portinfo = "%s port : %s   state : %s   product : %s
" % (

info, port, tmp['scan'][host]['tcp'][port]['state'], tmp['scan'][host]['tcp'][port]['product'])

result = result + portinfo

except KeyError, e:

if whitelist:

whitestr = ','.join(whitelist)

result = result + "未扫到开放端口!请检查%s端口对应的服务状态" % whitestr

else:

result = result + "扫描结果正常,无暴漏端口"

return result

def help():

print "Usage: nmScan(['127.0.0.1',],'0-65535')"

return None

if __name__ == "__main__":

hostlist = ['10.10.10.10', '10.10.10.11']

portrange = '0-65535'

whitelist = [80, 443]

l = nmScan(hostlist, portrange, whitelist)

sender = '75501664@qq.com'

receiver = ['zhangyanlin8851@163.com', '877986976@qq.com']

subject = '服务器端口扫描'

smtpserver = 'smtp.exmail.qq.com'

smtpuser = 'zhangyanlin8851@163.cn'

smtppass = 'linuxidc163'

mailcontent = ''

for i in range(len(l)):

mailcontent = mailcontent + l[i][1]

tool.sendemail(sender, receiver, subject, mailcontent, smtpserver, smtpuser, smtppass)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值