Python實現連接埠掃描

來源:互聯網
上載者:User

標籤:連接埠掃描

誤刪了之前的shell指令碼,之前的shell指令碼因為擴充性不強,就打算用python來重新實現。

連接埠掃描的方法特別多,我這裡是把結果處理成json格式,交給後端的django來處理。


#!/usr/bin/env python# -*- coding: utf-8 -*-"""Date:2018-05-14Author:BobDescription:Processing nmap scan results"""import osimport timeimport jsonimport requestsimport subprocessfrom xml.etree import ElementTree as ETurl = 'http://10.200.56.80:8000/portscan/portScanInterface/'def remove_file(del_file):    if os.path.exists(del_file):        os.remove(del_file)        return del_filedef alive_ip():    # Detecting live ip    with open('ip_subnet.txt', 'r') as f:        for ip in f:            ip = ip.strip()            cmd = '/usr/bin/nmap -sP -PI -PT %s >> alive_ip.txt' %ip            p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)            print p.stdout.read(),    # Scan live ip    ips = []    with open('alive_ip.txt', 'r') as f:        for lines in f:            if lines.startswith('Nmap scan'):                ip = lines.split(' ')                ip = ip[4].strip()                ips.append(ip)    ip_str = ' '.join(ips)    nmap_scan = 'nmap -sV -oX nmap_scan_output.xml %s > /dev/null 2>&1' %ip_str    p = subprocess.Popen(nmap_scan, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)    print p.stdout.read(),def deal_scan_xml(xml_file):    # Processing nmap scan results    bd = open(xml_file, 'r').read()    root = ET.fromstring(bd)    data = []    for host in root.findall('host'):        # Print the number of child elements in the host tag        # print len(host),        # Get ip address        ip_ = host[1].get('addr')        # Get ports, protocols, and other information        if len(host) == 5:            for port in host[3][0:]:                # print port                port_ = str(port.get('portid'))                protocol_ = str(port.get('protocol'))                if port[0].tag == 'extrareasons':                    continue                state_ = port[0].get('state')                service_ = str(port[1].get('name'))                product_ = str(port[1].get('product'))                version_ = str(port[1].get('version'))                extrainfo_ = str(port[1].get('extrainfo'))                ip_ = ip_                data.append({"ip": ip_, "port": port_, "protocol": protocol_, "state": state_, "service": service_,                             "product": product_, "version": version_, "extrainfo": extrainfo_})    json_data = json.dumps({"detail": data})    json_data = requests.post(url, {"detail": json_data})    print json_data.textdef main():    remove_file('alive_ip.txt')    remove_file('nmap_scan_output.xml')    alive_ip()    deal_scan_xml('nmap_scan_output.xml')if __name__ == '__main__':    main()


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.