示例#1
0
文件: test_core.py 项目: suakow/jaide
def op_commands(session, op_list):
    """ Run operational commands. """
    # Try a single op command
    print color('Single op command: show interfaces terse | match lo0', 'yel')
    print session.op_cmd('show interfaces terse | match lo0')

    # single op command with xml output
    print color(
        'Single op command in xml format: show interfaces terse'
        ' | match lo0', 'yel')
    print session.op_cmd('show interfaces terse | match lo0', req_format='xml')

    # single op command with xpath filtering
    print color(
        'Single op command with xpath filtering on package-'
        'information: show version', 'yel')
    print session.op_cmd('show version', xpath_expr='//package-information')

    # Try a list of op commands, includes the following:
    #     regular command
    #     show version
    #     # command with piping
    #     show route | match 0.0.0.0
    print color('Multiple op commands from file:', 'yel')
    # loop over the commands in our file
    for cmd in clean_lines(op_list):
        print color(cmd, 'yel')
        print session.op_cmd(cmd)
示例#2
0
def op_commands(session, op_list):
    """ Run operational commands. """
    # Try a single op command
    print color('Single op command: show interfaces terse | match lo0', 'yel')
    print session.op_cmd('show interfaces terse | match lo0')

    # single op command with xml output
    print color('Single op command in xml format: show interfaces terse'
                ' | match lo0', 'yel')
    print session.op_cmd('show interfaces terse | match lo0', req_format='xml')

    # single op command with xpath filtering
    print color('Single op command with xpath filtering on package-'
                'information: show version', 'yel')
    print session.op_cmd('show version', xpath_expr='//package-information')

    # Try a list of op commands, includes the following:
    #     regular command
    #     show version
    #     # command with piping
    #     show route | match 0.0.0.0
    print color('Multiple op commands from file:', 'yel')
    # loop over the commands in our file
    for cmd in clean_lines(op_list):
        print color(cmd, 'yel')
        print session.op_cmd(cmd)
示例#3
0
文件: test_core.py 项目: suakow/jaide
def shell_commands(session, shell_list):
    """ Run shell commands. """
    # try a single shell command
    print color('Single shell command: pwd', 'yel')
    print session.shell_cmd('pwd')

    # try a shell command file
    # file contents:
    #    pwd
    #    cd /var/tmp
    #    pwd
    #    ls -lap
    #    touch my-new-file
    #    ls -lap
    print color('multiple shell commands from file:', 'yel')
    # loop over the commands in our file.
    for cmd in clean_lines(shell_list):
        print color(cmd, 'yel')
        print session.shell_cmd(cmd)
示例#4
0
def shell_commands(session, shell_list):
    """ Run shell commands. """
    # try a single shell command
    print color('Single shell command: pwd', 'yel')
    print session.shell_cmd('pwd')

    # try a shell command file
    # file contents:
    #    pwd
    #    cd /var/tmp
    #    pwd
    #    ls -lap
    #    touch my-new-file
    #    ls -lap
    print color('multiple shell commands from file:', 'yel')
    # loop over the commands in our file.
    for cmd in clean_lines(shell_list):
        print color(cmd, 'yel')
        print session.shell_cmd(cmd)
示例#5
0
    def run(self):
        """ Overwrite threading.Thread run method.

        Purpose: This overwrites threading.Thread's run() method. It is called
               | by doing WorkerThread.start(). The overaching goal is to
               | open a multiprocessing pool and execute the run_jaide()
               | function against each IP supplied to us.
               |
               | We callback to write_to_queue() for a list of IP addresses.
               | Once we have executed the script, we check the write_to_file
               | parameter to see if we need to output to a file.

        @returns: None
        """
        # build the list of IPs
        iplist = [ip for ip in clean_lines(self.ip)]
        for ip in iplist:
            # TODO: set back to mp_pool before finishing release. This is only set as is to receive errors.
            # self.write_to_queue(run_jaide(ip.strip(), self.username,
            #                               self.password, self.command,
            #                               self.sess_timeout, self.argsToPass,
            #                               self.conn_timeout, self.port))
            self.mp_pool.apply_async(run_jaide,
                                     args=(ip.strip(), self.username,
                                           self.password, self.command,
                                           self.sess_timeout, self.argsToPass,
                                           self.conn_timeout, self.port),
                                     callback=self.write_to_queue)
        self.mp_pool.close()
        self.mp_pool.join()

        if self.write_to_file:
            # Just dumping all output to a single file.
            if self.wtf_style in ["s", "single"]:
                try:
                    out_file = open(self.write_to_file, "a+b")
                except IOError as e:
                    self.stdout.put("Could not save script output to file."
                                    " Error:\n" + str(e))
                else:
                    while not self.wtfQueue.empty():
                        out_file.write(self.wtfQueue.get())
                    self.stdout.put("\nSuccessfully appended output to: "
                                    "%s\n" % self.write_to_file)
            # Dump output to one file for each IP touched.
            elif self.wtf_style in ["m", 'multiple']:
                temp_output = ""
                while not self.wtfQueue.empty():
                    temp_output += self.wtfQueue.get()
                temp_output = temp_output.split("=" * 50)
                for x in range(1, len(temp_output)):
                    # get each of the IP/hostnames we touched
                    ip = temp_output[x].split(
                        'Results from device: ')[1].split('\n')[0].strip()
                    # inject the ip into the front of the filename
                    filepath = path.join(
                        path.split(self.write_to_file)[0],
                        ip + "_" + path.split(self.write_to_file)[1])
                    try:
                        out_file = open(filepath, 'a+b')
                    # use stdout.put here instead of write_to_queue since it is
                    # not usable after we've already written everything
                    # to the file.
                    except IOError as e:
                        self.stdout.put('Error opening output file \'%s\' for'
                                        ' writing. The Error was:\n%s' %
                                        (filepath, str(e)))
                    else:
                        out_file.write(temp_output[x])
                        self.stdout.put('\nOutput written/appended to: ' +
                                        filepath)
                        out_file.close()
    def run(self):
        """ Overwrite threading.Thread run method.

        Purpose: This overwrites threading.Thread's run() method. It is called
               | by doing WorkerThread.start(). The overaching goal is to
               | open a multiprocessing pool and execute the run_jaide()
               | function against each IP supplied to us.
               |
               | We callback to write_to_queue() for a list of IP addresses.
               | Once we have executed the script, we check the write_to_file
               | parameter to see if we need to output to a file.

        @returns: None
        """
        # build the list of IPs
        iplist = [ip for ip in clean_lines(self.ip)]
        for ip in iplist:
            # TODO: set back to mp_pool before finishing release. This is only set as is to receive errors.
            # self.write_to_queue(run_jaide(ip.strip(), self.username,
            #                               self.password, self.command,
            #                               self.sess_timeout, self.argsToPass,
            #                               self.conn_timeout, self.port))
            self.mp_pool.apply_async(run_jaide, args=(ip.strip(),
                                                      self.username,
                                                      self.password,
                                                      self.command,
                                                      self.sess_timeout,
                                                      self.argsToPass,
                                                      self.conn_timeout,
                                                      self.port),
                                     callback=self.write_to_queue)
        self.mp_pool.close()
        self.mp_pool.join()

        if self.write_to_file:
            # Just dumping all output to a single file.
            if self.wtf_style in ["s", "single"]:
                try:
                    out_file = open(self.write_to_file, "a+b")
                except IOError as e:
                    self.stdout.put("Could not save script output to file."
                                    " Error:\n" + str(e))
                else:
                    while not self.wtfQueue.empty():
                        out_file.write(self.wtfQueue.get())
                    self.stdout.put("\nSuccessfully appended output to: "
                                    "%s\n" % self.write_to_file)
            # Dump output to one file for each IP touched.
            elif self.wtf_style in ["m", 'multiple']:
                temp_output = ""
                while not self.wtfQueue.empty():
                    temp_output += self.wtfQueue.get()
                temp_output = temp_output.split("=" * 50)
                for x in range(1, len(temp_output)):
                    # get each of the IP/hostnames we touched
                    ip = temp_output[x].split('Results from device: ')[1].split('\n')[0].strip()
                    # inject the ip into the front of the filename
                    filepath = path.join(path.split(self.write_to_file)[0],
                                         ip + "_" +
                                         path.split(self.write_to_file)[1])
                    try:
                        out_file = open(filepath, 'a+b')
                    # use stdout.put here instead of write_to_queue since it is
                    # not usable after we've already written everything
                    # to the file.
                    except IOError as e:
                        self.stdout.put('Error opening output file \'%s\' for'
                                        ' writing. The Error was:\n%s' %
                                        (filepath, str(e)))
                    else:
                        out_file.write(temp_output[x])
                        self.stdout.put('\nOutput written/appended to: ' +
                                        filepath)
                        out_file.close()