<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    qileilove

    blog已經轉移至github,大家請訪問 http://qaseven.github.io/

    新手學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:\portscan\config.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:\portscan\scanres\\"
    nmapcmd=nmapath+' -PT '+ip.strip('\r\n')+' -oX '+filepath+filename
    os.system(nmapcmd)
    scaniplist.append(ip)
    writeinmysql(scaniplist)
    #入庫模塊是某大嬸發寫好的給我 我只是簡單修改了哈,主要是xml.etree.ElementTree模塊。
    def writeinmysql(scaniplist):
    filepath=r"c:\portscan\scanres"
    for ip in scaniplist:
    xmlfile=filepath+'\\'+ip+'.xml'
    #縮進哈 發文章的時候臨時改的,懶得縮進了
    root=ET.parse(xmlfile).getroot()
    allhost=root.findall('host')
    conn=MySQLdb.connect(host='10.1.11.11',user='nxadmin',passwd='nxadmin.com',port=3306,db='scandatabase',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:\portscan\config.ini中主要是api接口主url,nmap安裝路徑。

    posted on 2014-11-18 09:47 順其自然EVO 閱讀(299) 評論(0)  編輯  收藏 所屬分類: 測試學習專欄

    <2014年11月>
    2627282930311
    2345678
    9101112131415
    16171819202122
    23242526272829
    30123456

    導航

    統計

    常用鏈接

    留言簿(55)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产成人久久综合一区77 | 亚洲一区二区三区乱码A| 一级做a免费视频观看网站| 久久精品国产亚洲AV网站| 日韩亚洲国产高清免费视频| 亚洲AV无码专区国产乱码不卡| 亚洲精品天堂成人片?V在线播放| 午夜免费福利片观看| 亚洲av无码无线在线观看| 久久久青草青青亚洲国产免观| 在线观看视频免费完整版| 韩国免费a级作爱片无码| 亚洲国产一区在线观看| 亚洲国产精品成人久久蜜臀 | 精品久久久久久亚洲精品| 亚洲精品久久久www| 久久久久久久免费视频| 两性色午夜免费视频| 亚洲中文字幕久久精品蜜桃| 亚洲色大成网站www永久一区| 成人免费视频一区二区三区| 热久久这里是精品6免费观看| 亚洲欧美黑人猛交群| 亚洲av日韩综合一区在线观看| 国产最新凸凹视频免费| 日本免费xxxx色视频| 在线观看免费无码视频| 国产AV无码专区亚洲AV麻豆丫| 亚洲日本视频在线观看| 亚洲乱码精品久久久久..| 国产免费久久精品久久久| 免费毛片a在线观看67194| 两个人看www免费视频| 另类专区另类专区亚洲| 亚洲91精品麻豆国产系列在线| 国产亚洲欧洲精品| 亚洲精品专区在线观看| 国产精品成人四虎免费视频| 日本妇人成熟免费中文字幕 | 国产精品成人免费视频网站京东| 野花香高清在线观看视频播放免费 |