设为首页 加入收藏

TOP

Python 使用python-nmap模块实现端口扫描器(二)
2015-02-15 13:01:55 来源: 作者: 【 】 浏览:104
Tags:Python 使用 python-nmap 模块 实现 扫描器

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.1','10.10.10.2']
? ? pool = Pool(5)
? ? nmargu = partial(nmScan,portrange='0-65535',whitelist=[])
? ? results = pool.map(nmargu,hostlist)
? ? #send email
? ? sender = 'linuxidc@163.com'
? ? receiver = ['linuxidc@qq.com',]
? ? subject = '服务器端口扫描'
? ? smtpserver = 'smtp.exmail.qq.com'
? ? smtpuser = 'linuxidc@163.com'
? ? smtppass = 'linuxidc163'
? ? mailcontent = '
'.join(results)
? ? tool.sendemail(sender,receiver,subject,mailcontent,smtpserver,smtpuser,smtppass)


扫描结果:马赛克阻碍了人类文明的进步,尤其是在欣赏岛国动作片的时候,但是,亲,我不能把俺们的服务器给你看的,你懂的!


Python 使用python-nmap模块实现端口扫描器


--------------------------------------分割线 --------------------------------------


首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C、Shell混合编程小技巧 下一篇Java简单生成二维码

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: