Example #1
0
    def readSelectorPaired(self, options):
        """Parse paired reads"""
        FP = FileParser()
        # we need to find the file type (fasta, fasta.gz, etc...)
        file_type = os.path.splitext(options.files[0].replace('.gz',''))[1]
        if not options.large:
            file_type += '.gz'
            fopen = gzip.open
        else:
            fopen = open
        if options.prefix[0] == '':
            # stdout!
            outfh_1 = sys.stdout
            outfh_2 = sys.stdout
        else:
            try:
                outfh_1 = fopen("%s_1%s" % (options.prefix[0], file_type), "w") 
                outfh_2 = fopen("%s_2%s" % (options.prefix[0], file_type), "w") 
            except:
                raise FileOpenException("Could not open output files!") 

        # extract the reads
        FP.extractPairedReads(options.files, [outfh_1, outfh_2], options.number, options.verbose)

        # clean up
        outfh_1.close()
        outfh_2.close()
Example #2
0
    def readSelectorUnpaired(self, options, shuffled=False):
        """Parse unpaired or shuffled reads"""
        FP = FileParser()

        if options.outfile[0] == '':
            # stdout!
            outfh = sys.stdout
        else:
            # check if we need to zip output
            if options.outfile[0].endswith('.gz'):
                fopen = gzip.open
            else:
                fopen = open

            try:
                outfh = fopen(options.outfile[0], "w") 
            except:
                raise FileOpenException("Could not open output file!") 
    
        # extract the reads
        if shuffled:
            FP.extractShuffledReads(options.files, outfh, options.number, options.verbose)
        else:
            FP.extractUnpairedReads(options.files, outfh, options.number, options.verbose)
        
        # clean up
        outfh.close()
Example #3
0
    def test_invalid_file(self):
        # Arrange

        # Act
        fileParser = FileParser('invalidFileName.blah')
        try:
            fileParser.parse()
            self.assertEqual(1, 2)
        except Exception:
            # Assert
            self.assertTrue
Example #4
0
 def __init__(self, filename, command, target):
     self.filename = filename
     self.command = command
     self.target = target
     self.nmap_version = ""
     self.timestamp = ""
     self.total_devices = 0
     self.total_vulnerabilities = 0
     self.devices = []
     self.fp = FileParser("nmap_scan_unparsed.txt")
     self.startReport()
Example #5
0
    def test_valid_file(self):
        # Arrange
        self.create_file(self.validData)

        # Act
        filePaser = FileParser(self.testFilePath)
        try:
            filePaser.parse()
            self.assertEqual(1, 1)
        except Exception:
            self.assertEqual(1, 2)

        # Clean up
        os.remove(self.testFilePath)
Example #6
0
 def close(self):
     print "MyFile.close()!\n%s" % self.data
     fields = FileParser().parse(self.data)
     if self.app.protocol is not None:
         for x in fields:
             self.app.protocol.sendOrderFromFragments(x)
import sys
from fileParser import FileParser
from report import Report
from errorLog import ErrorLog

errorLog = ErrorLog()
errorLog.setPath("error.log")

# Check for an argument that should be a file name
if len(sys.argv) == 1:
    errorLog.log('Please provide the path to an input file')
    print('Please provide the path to an input file')
else:
    parser = FileParser(sys.argv[1])
    data = parser.parse()

    report = Report(data)
    # check if a file is provided for the output report
    if len(sys.argv) > 2:
        report.printReport(sys.argv[2])
    # output the record to the screen
    else:
        report.printReport()
Example #8
0
class Report:
    def __init__(self, filename, command, target):
        self.filename = filename
        self.command = command
        self.target = target
        self.nmap_version = ""
        self.timestamp = ""
        self.total_devices = 0
        self.total_vulnerabilities = 0
        self.devices = []
        self.fp = FileParser("nmap_scan_unparsed.txt")
        self.startReport()

    def startReport(self):
        if (self.fp.openFile()):
            self.nmap_version = self.fp.getNmapVersion()
            self.timestamp = self.fp.getTimestamp()
            self.devices = self.fp.getMachinesFound()
            self.total_devices = len(self.devices)
            self.total_vulnerabilities = self.getTotalVulnerabilities()
            self.getSummaryReport()

    def getTotalVulnerabilities(self):
        total = 0
        for dev in self.devices:
            open_ports = dev.totals['open_ports']
            filtered_ports = dev.totals['filtered_ports']
            total += open_ports + filtered_ports
        return total

    def getDeviceList(self):
        device_list = ""
        ix = 0
        for dev in self.devices:
            ip_addr = dev.ip_addr
            if dev.host_desc == "":
                host = dev.host
            else:
                host = dev.host_desc
            device_list += f"\t{dev.ip_addr}\t|\t{host}\n"

            if ix < len(self.devices) - 1:
                device_list += f"\t" + "-" * 16 + "+" + "-" * 50 + "\n"
            ix += 1
        return device_list

    def writeReport(self, summaryReport):
        str = ""
        str += summaryReport
        str += self.fp.displayMachines()
        try:
            f = open(self.filename, "w+")
            f.write(str)
        finally:
            f.close()

    def getSummaryReport(self):
        str = f"-" * 100 + "\n"
        str += f"Nmap Scan Report:\n"
        str += f"-" * 100 + "\n"
        str += f"                    Nmap Version:\t{self.nmap_version}\n"
        str += f"                    Nmap Command:\t{self.command}\n"
        str += f"                          Target:\t{self.target}\n"
        str += f"                       Timestamp:\t{self.timestamp}"
        str += f"-" * 100 + "\n"
        str += f"        Total # of Devices found:\t{self.total_devices}" + "\n"
        str += f"Total # of Vulnerabilities found:\t{self.total_vulnerabilities}" + "\n"
        str += f"-" * 100 + "\n"
        str += f"Device List:\n\n"
        str += f"\tip address\t|\tdevice name\n"
        str += f"\t" + "=" * 16 + "+" + "=" * 50 + "\n"
        str += self.getDeviceList()
        str += f"\t" + "-" * 16 + "+" + "-" * 50 + "\n\n"
        str += f"-" * 100 + "\n"
        str += f"For a detailed report, please view the file \'" + self.filename + "\' with a text editor\n"
        str += f"For the unparsed Nmap file, please view the file \'nmap_scan_unparsed.txt\' with a text editor\n"
        self.writeReport(str)
        return str
Example #9
0
# encoding: utf-8
from __future__ import unicode_literals
from fileParser import FileParser
file_name = "source/busy_day.in"
file_parser = FileParser(file_name)
file_parser.init()