日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python端口掃描系統(tǒng)實(shí)現(xiàn)方法

系統(tǒng) 1783 0

本文實(shí)例講述了python端口掃描系統(tǒng)實(shí)現(xiàn)方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

該程序的主要功能如下:

1. 從自有API接口獲取所有的外網(wǎng)IP段;
2. 用Nmap 遍歷掃描所有的IP段,-oX 生成XML的掃描報(bào)告;
3. 用xml.etree.ElementTree模塊方法讀取XML文件,將ip,開放端口,對應(yīng)服務(wù)等寫入Mysql數(shù)據(jù)庫。
功能很簡單,沒有滿足老大高大上的需求,所以這個(gè)小項(xiàng)目就這么英勇的掛掉了!~~~完全都還沒考慮程序異常終止,掃描服務(wù)器異常歇菜的情況。
貼上代碼:

復(fù)制代碼 代碼如下:
#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列表不為空,轉(zhuǎn)換成ip/網(wǎng)關(guān)格式,再格式化成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地址進(jìn)行去重
newiplist=[]
scaniplist=[]
for ip in iplist:
if ip not in newiplist:
newiplist.append(ip)
#遍歷所有ip段,批量掃描,生成xml格式報(bào)告
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)
?
#入庫模塊是某大嬸發(fā)寫好的給我 我只是簡單修改了哈,主要是xml.etree.ElementTree模塊。
def writeinmysql(scaniplist):
filepath=r"c:portscanscanres"
for ip in scaniplist:
xmlfile=filepath+'\'+ip+'.xml'
#縮進(jìn)哈 發(fā)文章的時(shí)候臨時(shí)改的,懶得縮進(jìn)了
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的,如果是再進(jìn)行入庫
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安裝路徑。

感興趣的朋友可以進(jìn)一步完善該實(shí)例的功能。希望本文所述對大家的Python程序設(shè)計(jì)有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 泸州市| 巫溪县| 大洼县| 古田县| 北辰区| 新营市| 乌兰察布市| 吴江市| 寿光市| 望谟县| 大新县| 泰安市| 汝阳县| 永和县| 沧源| 北海市| 稻城县| 荥阳市| 黑山县| 景宁| 化州市| 寿阳县| 南阳市| 织金县| 孟津县| 洛阳市| 得荣县| 丹寨县| 万全县| 临朐县| 建昌县| 北宁市| 香港 | 冷水江市| 临澧县| 仁怀市| 西盟| 凯里市| 定南县| 永定县| 西乌珠穆沁旗|