Exemple #1
0
    def parse(self, input):
        self.words = input.strip(".").split(" ")
        self.words = [e.lower() for e in self.words if e != '']
        self.verb = None
        self.noun = None

        for word in self.words:
            for key in self.verb_map.keys():
                if word in key:
                    self.verb = self.verb_map[key]
                    self.words.remove(word)
            if self.verb != None:
                break

        for word in self.words:
            if word in self.nouns and not self.noun:
                self.noun = word
                self.words.remove(word)

        # Check there is at least a verb
        if self.verb == None:
            raise InvalidCommandError("You need a verb!\n")
        # Check no other words are verbs or nouns
        for word in self.words:
            if word in self.nouns:
                raise InvalidCommandError("Too many nouns!\n")
            for key in self.verb_map.keys():
                if word in key:
                    raise InvalidCommandError("Too many verbs!\n")

        return (self.verb, self.noun)
Exemple #2
0
    def shell_cmd(self, command=""):
        """ Execute a shell command.

        Purpose: Used to send a shell command to the connected device.
               | This uses the self._shell instance, which should be a
               | paramiko.Channel object, instead of a SSHClient.
               | This is because we cannot send shell commands to the
               | device using a SSHClient.

        @param command: The single command that to retrieve output from the
                      | device.
        @type command: str

        @returns: The reply from the device.
        @rtype: str
        """
        if not command:
            raise InvalidCommandError("Parameter 'command' must not be empty.")
        command = command.strip() + '\n'
        self._shell.send(command)
        time.sleep(2)
        out = ''
        while self._shell.recv_ready():
            out += self._shell.recv(999999)
            time.sleep(.75)
        # take off the command being sent and the prompt at the end.
        return '\n'.join(out.split('\n')[1:-1])
Exemple #3
0
    def compare_config(self, commands="", req_format="text"):
        """ Execute a 'show | compare' against the specified commands.

        Purpose: This method will take in string of multiple commands,
               | and perform and 'show | compare' on the device to show the
               | differences between the active running configuration and
               | the changes proposed by the passed commands parameter.

        @param commands: A string, filepath, or list of multiple commands
                       | that the device will compare with.
        @type commands: str or list
        @param req_format: The desired format of the response, defaults to
                         | 'text', but also accepts 'xml'
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        if not commands:
            raise InvalidCommandError('No commands specified')
        clean_cmds = [cmd for cmd in clean_lines(commands)]
        self.lock()
        self._session.load_configuration(action='set', config=clean_cmds)
        out = self._session.compare_configuration()
        self.unlock()
        if req_format.lower() == "xml":
            return out
        return out.xpath(
            'configuration-information/configuration-output')[0].text
Exemple #4
0
    def op_cmd(self, command, req_format='text', xpath_expr=""):
        """ Execute an operational mode command.

        Purpose: Used to send an operational mode command to the connected
               | device. This requires and uses a paramiko.SSHClient() as
               | the handler so that we can easily pass and allow all pipe
               | commands to be used.
               |
               | We indiscriminately attach ' | no-more' on the end of
               | every command so the device doesn't hold output. The
               | req_format parameter can be set to 'xml' to force raw
               | xml output in the reply.

        @param command: The single command that to retrieve output from the
                      | device. Any pipes will be taken into account.
        @type command: str
        @param req_format: The desired format of the response, defaults to
                         | 'text', but also accepts 'xml'. **NOTE**: 'xml'
                         | will still return a string, not a libxml ElementTree
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        if not command:
            raise InvalidCommandError("Parameter 'command' cannot be empty")
        if req_format.lower() == 'xml' or xpath_expr:
            command = command.strip() + ' | display xml'
        command = command.strip() + ' | no-more\n'
        out = ''
        # when logging in as root, we use _shell to get the response.
        if self.username == 'root':
            self._shell.send(command)
            time.sleep(3)
            while self._shell.recv_ready():
                out += self._shell.recv(999999)
                time.sleep(.75)
            # take off the command being sent and the prompt at the end.
            out = '\n'.join(out.split('\n')[1:-2])
        # not logging in as root, and can grab the output as normal.
        else:
            stdin, stdout, stderr = self._session.exec_command(command=command,
                                           timeout=float(self.session_timeout))
            stdin.close()
            # read normal output
            while not stdout.channel.exit_status_ready():
                out += stdout.read()
            stdout.close()
            # read errors
            while not stderr.channel.exit_status_ready():
                out += stderr.read()
            stderr.close()
        return out if not xpath_expr else xpath(out, xpath_expr)
Exemple #5
0
    def commit_check(self, commands="", req_format="text"):
        """ Execute a commit check operation.

        Purpose: This method will take in string of multiple commands,
               | and perform and 'commit check' on the device to ensure
               | the commands are syntactically correct. The response can
               | be formatted as text or as xml.

        @param commands: A string, filepath, or list of multiple commands
                       | that the device will compare with.
        @type commands: str or list
        @param req_format: The desired format of the response, defaults to
                         | 'text', but also accepts 'xml'
        @type req_format: str

        @returns: The reply from the device.
        @rtype: str
        """
        if not commands:
            raise InvalidCommandError('No commands specified')
        clean_cmds = []
        for cmd in clean_lines(commands):
            clean_cmds.append(cmd)
        self.lock()
        self._session.load_configuration(action='set', config=clean_cmds)
        # conn.validate() DOES NOT return a parse-able xml tree, so we
        # convert it to an ElementTree xml tree.
        results = ET.fromstring(self._session.validate(
            source='candidate').tostring)
        # release the candidate configuration
        self.unlock()
        if req_format == "xml":
            return ET.tostring(results)
        out = ""
        # we have to parse the elementTree object, and get the text
        # from the xml.
        for i in results.iter():
            # the success message is just a tag, so we need to get it
            # specifically.
            if i.tag == 'commit-check-success':
                out += 'configuration check succeeds\n'
            # this is for normal output with a tag and inner text, it will
            # strip the inner text and add it to the output.
            elif i.text is not None:
                if i.text.strip() + '\n' != '\n':
                    out += i.text.strip() + '\n'
            # this is for elements that don't have inner text, it will add the
            # tag to the output.
            elif i.text is None:
                if i.tag + '\n' != '\n':
                    out += i.tag + '\n'
        return out
Exemple #6
0
def UserOptions():

    # Clear terminal window
    os.system('CLS')
    print('\n   ')
    # Display RootHash title as ASCII art
    print(COLOR + Style.BRIGHT)
    tprint('RootHash', FONT)
    print(Fore.RESET)
    print('\n')

    # This part get the RootHash owner name from settings file
    Owner = ''

    # Open settings file
    with open(Spath, 'r') as F:
        Owner = F.read()

    k = Owner.split('\n')

    # Get owner name into variable
    Owner = k[0]

    # RootHash user options in welcome screen
    print(f' Welcome To RootHash {Owner}')
    print(' \t[1] Add New Record')
    print(' \t[2] Modify Record')
    print(' \t[3] Delete Record')
    print(' \t[4] View All Records')
    print(' \t[5] Change Root Password')
    print(' \t[6] About RootHash')
    print(' \t[7] Exit\n')

    try:
        # Get user command
        cmd = input('[>>] Your Command : ')

        if cmd == '1':
            RCD.new_entry(Owner, DPATH, COLOR, FONT)

        elif cmd == '2':
            RCD.modify(Owner, DPATH, COLOR, FONT)

        elif cmd == '3':
            RCD.delete_entry(Owner, DPATH, COLOR, FONT)

        elif cmd == '4':
            RCD.view_all(Owner, DPATH, COLOR, FONT)

        elif cmd == '5':
            OPT.change_mastercode(SPATH, COLOR, FONT)

        elif cmd == '6':
            CDT.about(COLOR, FONT)

        elif cmd == '7':
            sys.exit(2)

        else:
            raise InvalidCommandError("Invalid command")

    except InvalidCommandError as e:
        print(Fore.RED + '\n[!] Invalid Command')
        print(Fore.RESET)
        # Run windows PAUSE command
        os.system('PAUSE')
        UserOptions()

    # This part ignores 'Ctrl+C cancel operation'
    except KeyboardInterrupt:
        UserOptions()