Python-Nmap使用及案例
作者:张岩林 发布时间:[ 2016/10/28 15:13:10 ] 推荐标签:Python-Nmap 软件测试
实验案例
检测内网机器端口
1.定义函数库mytools.py
#-*- coding:utf-8 -*-
import smtplib
from email.mime.text import MIMEText
from email.header import Header
def sendemail(sender,receiver,subject,content,smtpserver,smtpuser,smtppass):
msg = MIMEText(content,'html','utf-8')#中文需参数‘utf-8',单字节字符不需要
msg['Subject'] = Header(subject, 'utf-8')
msg['From'] = '<%s>' % sender
msg['To'] = ";".join(receiver)
try:
smtp = smtplib.SMTP()
smtp.connect(smtpserver)
smtp.login(smtpuser, smtppass)
smtp.sendmail(sender, receiver, msg.as_string())
smtp.quit()
except Exception,e:
print e
2. 实现端口扫描的程序,单线程版本nmscan.py
# !/usr/bin/python
# -*- coding:utf-8 -*-
import nmap
import re
import mytools as tool
import sys
reload(sys)
sys.setdefaultencoding('utf8')
def nmScan(hostlist, portrange, whitelist):
p = re.compile("^(d*)-(d*)$")
if type(hostlist) != list:
help()
portmatch = re.match(p, portrange)
if not portmatch:
help()
l = []
for host in hostlist:
result = ''
nm = nmap.PortScanner()
tmp = nm.scan(host, portrange)
result = result + "<h2>ip地址:%s 主机名:[%s] ...... %s</h2><hr>" % (
host, tmp['scan'][host]['hostname'], tmp['scan'][host]['status']['state'])
try:
ports = tmp['scan'][host]['tcp'].keys()
except KeyError, e:
if whitelist:
whitestr = ','.join(whitelist)
result = result + "未扫到开放端口!请检查%s端口对应的服务状态" % whitestr
else:
result = result + "扫描结果正常,无暴漏端口"
for port in ports:
info = ''
if port not in whitelist:
info = '<strong><font color=red>Alert:非预期端口</font><strong> '
else:
info = '<strong><font color=green>Info:正常开放端口</font><strong> '
portinfo = "%s <strong>port</strong> : %s <strong>state</strong> : %s <strong>product<strong/> : %s <br>" % (
info, port, tmp['scan'][host]['tcp'][port]['state'],
tmp['scan'][host]['tcp'][port]['product'])
result = result + portinfo
l.append([host, str(result)])
return l
def help():
print "Usage: nmScan(['127.0.0.1',],'0-65535')"
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)
3.多线程版本
# !/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 + "<h2>ip地址:%s 主机名:[%s] ...... %s</h2><hr>" % (
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 = '<strong><font color=red>Alert:非预期端口</font><strong> '
else:
info = '<strong><font color=green>Info:正常开放端口</font><strong> '
portinfo = "%s <strong>port</strong> : %s <strong>state</strong> : %s <strong>product<strong/> : %s <br>" % (
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)
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系SPASVO小编(021-61079698-8054),我们将立即处理,马上删除。
相关推荐
软件测试常用的工具都有哪些?国内外软件测试工具都在这里了2017年常用软件测试工具学习教程大全AutoRunner自动化软件测试工具2016年1月8日V4.0版本已正式发布!软件测试工具TestDirector规范软件测试工具WinRunner的问题整理软件测试工具WinRunner的规则软件测试工具Winrunner TSL命令简介软件测试工具WinRunner脚本测试标准格式使用软件测试工具WinRunner的几点建议软件测试工具WinRunner的工作流程对软件测试工具的认识常用软件测试工具的分析VectorCAST-自动化软件测试工具浅谈软件测试工具在工作中的作用软件测试工具的比较和选择软件测试工具速查手册
更新发布
功能测试和接口测试的区别
2023/3/23 14:23:39如何写好测试用例文档
2023/3/22 16:17:39常用的选择回归测试的方式有哪些?
2022/6/14 16:14:27测试流程中需要重点把关几个过程?
2021/10/18 15:37:44性能测试的七种方法
2021/9/17 15:19:29全链路压测优化思路
2021/9/14 15:42:25性能测试流程浅谈
2021/5/28 17:25:47常见的APP性能测试指标
2021/5/8 17:01:11热门文章
常见的移动App Bug??崩溃的测试用例设计如何用Jmeter做压力测试QC使用说明APP压力测试入门教程移动app测试中的主要问题jenkins+testng+ant+webdriver持续集成测试使用JMeter进行HTTP负载测试Selenium 2.0 WebDriver 使用指南