Python实现端口扫描

误删了之前的shell脚本,之前的shell脚本因为扩展性不强,就打算用python来重新实现。

端口扫描的方法特别多,我这里是把结果处理成json格式,交给后端的django来处理。

专注于为中小企业提供网站设计制作、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业开封免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了上千多家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Date:2018-05-14
Author:Bob
Description:Processing nmap scan results
"""

import os
import time
import json
import requests
import subprocess
from xml.etree import ElementTree as ET


url = '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_file

def 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 = '/usr/bin/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.text

def 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实现端口扫描
网页路径:http://bzwzjz.com/article/pjhseg.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 攀枝花网站设计 手机网站制作 外贸营销网站建设 盐亭网站设计 成都网站制作 成都营销网站制作 重庆企业网站建设 高端网站设计 成都网站制作 成都网站设计公司 高端定制网站设计 手机网站设计 营销网站建设 响应式网站设计 泸州网站建设 营销型网站建设 企业网站设计 做网站设计 成都网站建设公司 成都网站设计 成都企业网站建设 成都网站制作