最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

python端口扫描系统实现方法

来源:动视网 责编:小采 时间:2020-11-27 14:31:28
文档

python端口扫描系统实现方法

python端口扫描系统实现方法:本文实例讲述了python端口扫描系统实现方法。分享给大家供大家参考。具体实现方法如下: 该程序的主要功能如下: 1. 从自有API接口获取所有的外网IP段; 2. 用Nmap 遍历扫描所有的IP段,-oX 生成XML的扫描报告; 3. 用xml.etree.ElementTr
推荐度:
导读python端口扫描系统实现方法:本文实例讲述了python端口扫描系统实现方法。分享给大家供大家参考。具体实现方法如下: 该程序的主要功能如下: 1. 从自有API接口获取所有的外网IP段; 2. 用Nmap 遍历扫描所有的IP段,-oX 生成XML的扫描报告; 3. 用xml.etree.ElementTr


本文实例讲述了python端口扫描系统实现方法。分享给大家供大家参考。具体实现方法如下:

该程序的主要功能如下:

1. 从自有API接口获取所有的外网IP段;
2. 用Nmap 遍历扫描所有的IP段,-oX 生成XML的扫描报告;
3. 用xml.etree.ElementTree模块方法读取XML文件,将ip,开放端口,对应服务等写入Mysql数据库。
功能很简单,没有满足老大高大上的需求,所以这个小项目就这么英勇的挂掉了!~~~完全都还没考虑程序异常终止,扫描服务器异常歇菜的情况。
贴上代码:
代码如下:

#coding:utf-8
import sys,os,time,subprocess
import MySQLdb
import re,urllib2
import ConfigParser
from IPy import IP
import xml.etree.ElementTree as ET

nowtime = time.strftime('%Y-%m-%d',time.localtime(time.time()))
configpath=r'c:portscanconfig.ini'
#传入api接口主路径,遍历获取所有的ip列表,用IPy模块格式成127.0.0.1/24的格式
def getiplist(ipinf):
serverarea=['tj101','tj103','dh','dx']
iplist=[]
for area in serverarea:
ipapi=urllib2.urlopen(ipinf+area).read()
for ip in ipapi.split('n'):
#判断如果ip列表不为空,转换成ip/网关格式,再格式化成ip/24的格式
if ip:
ip=ip.replace('_','/')
ip=(IP(ip))
iplist.append(str(ip))
ipscan(iplist,nmapathx)

#传递ip地址文件和nmap路径
def ipscan(iplist,nmapath):
#古老的去重,对ip文件中的ip地址进行去重
newiplist=[]
scaniplist=[]
for ip in iplist:
if ip not in newiplist:
newiplist.append(ip)
#遍历所有ip段,批量扫描,生成xml格式报告
for ip in newiplist:
filename=nowtime+ip.split('/')[0]+'.xml'
filepath=r"c:portscanscanres\"
nmapcmd=nmapath+' -PT '+ip.strip('rn')+' -oX '+filepath+filename
os.system(nmapcmd)
scaniplist.append(ip)
writeinmysql(scaniplist)

#入库模块是某大婶发写好的给我 我只是简单修改了哈,主要是xml.etree.ElementTree模块。
def writeinmysql(scaniplist):
filepath=r"c:portscanscanres"
for ip in scaniplist:
xmlfile=filepath+'\'+ip+'.xml'
#缩进哈 发文章的时候临时改的,懒得缩进了
root=ET.parse(xmlfile).getroot()
allhost=root.findall('host')
conn=MySQLdb.connect(host='10.5.10.57',user='nxadmin',passwd='nxadmin.com',port=3306,db='scandb',charset='utf8')
cur= conn.cursor()
for host in allhost:
address = host.find('address')
#首先判断端口是不是open的,如果是再进行入库
for port in host.find('ports').findall('port'):
if port.find('state').attrib['state']=="open":
ip=address.attrib['addr']
portval=port.attrib['portid']
state=port.find('state').attrib['state']
sql = "INSERT INTO portscan (ip,port,state) VALUES(%s,%s,%s)"
params=[ip,portval,state]
cur.execute(sql,params)
conn.commit()
cur.close()
conn.close()
if __name__=="__main__":
#读取配置文件中要扫描的IP apiurl和nmap安装文件路径
config=ConfigParser.ConfigParser()
config.readfp(open(configpath,'rb'))
nmapathx=config.get('nmap','nmapath')
ipinf=config.get('ip','ipinf')
getiplist(ipinf)


配置文件c:portscanconfig.ini中主要是api接口主url,nmap安装路径。

感兴趣的朋友可以进一步完善该实例的功能。希望本文所述对大家的Python程序设计有所帮助。

文档

python端口扫描系统实现方法

python端口扫描系统实现方法:本文实例讲述了python端口扫描系统实现方法。分享给大家供大家参考。具体实现方法如下: 该程序的主要功能如下: 1. 从自有API接口获取所有的外网IP段; 2. 用Nmap 遍历扫描所有的IP段,-oX 生成XML的扫描报告; 3. 用xml.etree.ElementTr
推荐度:
标签: 方法 系统 扫描
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top