Esempio n. 1
0
    def simulate(self, cmds):
        """Submit scan to scan server for simulation
        
        :param cmds: List of commands,
                     :class:`~scan.commands.commandsequence.CommandSequence`
                     or text with raw XML format.
        
        :return: Simulation result as dictionary `{ 'simulation': "Printable text", 'seconds': 193.0 }`
        
        Example::

        >>> result = client.simulate([ Set('x', 5), Delay(2) ])
        >>> print result['simulation']
        """
        if isinstance(cmds, str):
            scan = cmds
        elif isinstance(cmds, CommandSequence):
            scan = cmds.genSCN()
        else:
            # Warp list, tuple, other iterable
            scan = CommandSequence(cmds).genSCN()

        url = self.__baseURL + "/simulate"

        result = perform_request(url, 'POST', scan)
        xml = ET.fromstring(result)
        if xml.tag != 'simulation':
            raise Exception("Expected scan <simulation>, got <%s>" % xml.tag)
        simulation = xml.find('log').text
        seconds = float(xml.find('seconds').text)
        return {'simulation': simulation, 'seconds': seconds}
Esempio n. 2
0
    def submit(self, cmds, name='UnNamed', queue=True):
        """Submit scan to scan server for execution
        
        :param cmds: List of commands,
                     :class:`~scan.commands.commandsequence.CommandSequence`
                     or text with raw XML format.
        :param name: Name of scan
        :param queue: Submit to scan server queue, or execute as soon as possible?
        
        :return: ID of submitted scan
        
        Examples::
        
        >>> cmds = [ Comment('Hello'), Set('x', 10) ]
        >>> id = client.submit( cmds, "My First Scan")
        
        >>> cmds = CommandSequent(Comment('Hello'))
        >>> cmds.append(Set('x', 10))
        >>> id = client.submit( cmds, "My Second Scan")
        """
        quoted_name = urllib.quote(name, '')
        if isinstance(cmds, str):
            result = self.__submitScanXML(cmds, quoted_name, queue)
        elif isinstance(cmds, CommandSequence):
            result = self.__submitScanSequence(cmds, quoted_name, queue)
        else:
            # Warp list, tuple, other iterable
            result = self.__submitScanSequence(CommandSequence(cmds),
                                               quoted_name, queue)

        xml = ET.fromstring(result)
        if xml.tag != 'id':
            raise Exception("Expected scan <id>, got <%s>" % xml.tag)
        return int(xml.text)
Esempio n. 3
0
    def submit(self,
               cmds,
               name='UnNamed',
               queue=True,
               timeout=0,
               deadline=None,
               pre_post=True):
        """Submit scan to scan server for execution
        
        :param cmds: List of commands,
                     :class:`~scan.commands.commandsequence.CommandSequence`
                     or text with raw XML format.
        :param name: Name of scan
        :param queue: Submit to scan server queue, or execute as soon as possible?
        :param timeout: Timeout in seconds after which scan will self-abort
        :param deadline: Execution deadline in "yyyy-MM-dd HH:mm:ss" format when scan will self-abort
        :param pre_post: Execute pre- and post-scan commands (default: True)
        
        :return: ID of submitted scan

        By default, a submitted scan will be queued.
        One scan is then executed at a time, in order of submission.
        Each scan is allowed to run until all its commands complete.

        The 'queue' parameter allows submitting scans for immediate execution,
        i.e. in parallel to the queued commands.

        Either the 'timeout' or the 'deadline' might be used to limit
        the execution time.
        
        Examples::
        
        >>> cmds = [ Comment('Hello'), Set('x', 10) ]
        >>> id = client.submit(cmds, "My First Scan")
        
        >>> cmds = CommandSequence(Comment('Hello'))
        >>> cmds.append(Set('x', 10))
        >>> id = client.submit(cmds, "My Second Scan")
        
        >>> cmds = CommandSequence(Delay(600))
        >>> id = client.submit(cmds, "Timeout", timeout=10)
        """
        quoted_name = quote(name, '')
        if isinstance(cmds, str):
            result = self.__submitScanXML(cmds, quoted_name, queue, timeout,
                                          deadline, pre_post)
        elif isinstance(cmds, CommandSequence):
            result = self.__submitScanSequence(cmds, quoted_name, queue,
                                               timeout, deadline, pre_post)
        else:
            # Warp list, tuple, other iterable
            result = self.__submitScanSequence(CommandSequence(cmds),
                                               quoted_name, queue, timeout,
                                               deadline, pre_post)

        xml = ET.fromstring(result)
        if xml.tag != 'id':
            raise Exception("Expected scan <id>, got <%s>" % xml.tag)
        return int(xml.text)
Esempio n. 4
0
        return commands

    def __repr__(self):
        """Returns table as columnized string."""
        result = 'headers=[ "'
        line = []
        for c in range(self.cols):
            line.append(self.headers[c].ljust(self.width[c]))
        result += '", "'.join(line)
        result += '" ]\nrows= ['
        for row in self.rows:
            line = []
            for c in range(self.cols):
                line.append(row[c].ljust(self.width[c]))
            result += ' [ "' + '", "'.join(line) + '" ],\n       '
        result += "]"
        return result


if __name__ == "__main__":
    from scan.commands.commandsequence import CommandSequence
    table = TableScan(["A", "B"], [["1", ""], ["2", "[5,6,7]"]])
    cmds = CommandSequence(table.createScan())
    print(table)
    print(cmds)

    table = TableScan(["A", "B"], [["Loop(1, 2, 1)", "loop(5, 7, 1)"]])
    cmds = CommandSequence(table.createScan())
    print(table)
    print(cmds)