国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python端口掃描系統實現方法

2020-02-23 06:10:37
字體:
來源:轉載
供稿:網友

本文實例講述了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)"

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 望城县| 洛南县| 诏安县| 简阳市| 泗阳县| 七台河市| 南乐县| 都兰县| 白银市| 朔州市| 天柱县| 长白| 宁城县| 逊克县| 湘潭县| 寿光市| 馆陶县| 鄂尔多斯市| 沈阳市| 逊克县| 固原市| 尉氏县| 涡阳县| 呼图壁县| 全州县| 楚雄市| 霸州市| 南丹县| 吴忠市| 什邡市| 望奎县| 徐水县| 华坪县| 榕江县| 井陉县| 仁寿县| 平潭县| 赣州市| 漳平市| 日喀则市| 清水县|