python連接埠掃描系統實現方法_python

來源:互聯網
上載者:User

本文執行個體講述了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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.