Example #1
0
 def show_brief(self, cli=False):
     if cli:
         print(color_string(self.CLI_HELP, 'yellow'))
     if self.custom_commands:
         print(color_string("  - List of your custom commands:", 'yellow'))
         for command in sorted(self.custom_commands):
             print(color_string(f'    * {command}: {self.custom_commands[command]["description"]}', 'yellow'))
Example #2
0
    def delete(self, command):
        res = self.custom_commands.pop(command, None)
        if not res:
            print(color_string("Command doesn't exit", 'red'))
            return

        self._save_to_file()
        print(color_string(f"Deleted command {command}", 'green'))
Example #3
0
 def show_details(self, command):
     for custom_command in sorted(self.custom_commands):
         if command in custom_command:
             print(color_string(f' * {custom_command}: {self.custom_commands[custom_command]["description"]}',
                                'yellow'))
             if 'args' in self.custom_commands[custom_command]:
                 print(color_string(f'   {" " * len(custom_command)}  '
                                    f'{self.custom_commands[custom_command]["args"]}',
                                    'yellow'))
Example #4
0
 def _ask_for_args(self):
     arguments = []
     print("Time to add arguments, hint: <arg_name> - <default_value>. Remember to end/save")
     user_input = ""
     while user_input.lower() not in self.EXIT_WORDS:
         user_input = input(color_string("=> ", 'cyan'))
         if user_input in ['']:
             continue
         if user_input.lower() not in self.EXIT_WORDS:
             try:
                 fields = user_input.split(":")
                 arguments.append((fields[0], fields[1]))
             except IndexError:
                 print(color_string("Your argument is not following the proper pattern:"
                                    " <arg_name> - <default_value>", 'red'))
     return arguments
Example #5
0
    def run(self):
        """
        Connect and run an interactive Netmiko session
        """
        try:
            self.connection = self._establish()
        except NetcliError as lacerror:
            self.queue.put((False, lacerror))
            return

        self.queue.put((True, ""))
        time.sleep(TIMEOUT)

        end_loop = False
        # Keeping the Netmiko session ongoing until user terminates cli session
        while not end_loop:
            requested_command = self.queue.get()[1]
            if requested_command.lower() in ['end', 'exit', 'quit']:
                end_loop = True
            elif requested_command.lower() in ['edit_command']:
                self.custom_commands = Config().__dict__['custom_commands']
                self.queue.put((True, ""))
            else:
                try:
                    response = self._execute_command(requested_command)
                    success = True
                except NetcliError as nce:
                    success, response = False, nce
                self.queue.put((success, response))
            time.sleep(TIMEOUT)

        bye_message = f"Disconnected from {self.config['ip']}. "
        if self.config['session_log']:
            bye_message += f"All your activity has been recorded in {self.config['session_log']}"
        print(color_string(bye_message, 'yellow'))
Example #6
0
    def _ask_vendor_commands(self):
        vendor_commands = {}
        print("Time to add vendor commands, hint: <type> - <command vrf [vrf]>. Remember to end/save")
        user_input = ""
        while user_input.lower() not in self.EXIT_WORDS:
            user_input = input(color_string("=> ", 'cyan'))
            if user_input in ['']:
                continue
            if user_input.lower() not in self.EXIT_WORDS:
                try:
                    vendor_type = user_input.split(" - ")[0]
                    if vendor_type not in CLASS_MAPPER_BASE:
                        print(color_string(f"Vendor type {vendor_type} not supported by Netmiko", 'red'))
                        continue

                    vendor_command = user_input.split(" - ")[1]
                    vendor_commands.update({vendor_type: vendor_command})
                except IndexError:
                    print(color_string("Your command is not following the proper pattern", 'red'))
        return vendor_commands
Example #7
0
    def add(self, command):
        """
        Example: "custom_command arg1:default arg2:555"
        """
        custom_command, args = self._get_custom_command_and_args(command)

        if custom_command in self.custom_commands:
            print(color_string(f"Sorry, custom command {custom_command} already there.", 'red'))
            return

        description = self._ask_description()

        vendor_commands = None
        resp = input("Do you want to add a concrete vendor implementation (y/N)?")
        if resp.lower() in ['y', 'yes']:
            vendor_commands = self._ask_vendor_commands()
        if not vendor_commands:
            print(color_string("Keep in mind that no actual implementation added", 'yellow'))

        self._update(custom_command, description, args, vendor_commands)

        print(color_string(f"Added command {command}", 'green'))
Example #8
0
    def edit(self, custom_command):
        if custom_command not in self.custom_commands:
            resp = input(f"Custom command {custom_command} not present yet. Do you want to add it (y/N)?")
            if resp.lower() not in ['y', 'yes']:
                return

        description, args, vendor_commands = None, None, None

        resp = input(f"Do you want to edit description (y/N)?")
        if resp.lower() in ['y', 'yes']:
            description = self._ask_description()

        resp = input(f"Do you want to edit arguments (y/N)?")
        if resp.lower() in ['y', 'yes']:
            args = self._ask_for_args()

        resp = input(f"Do you want to edit vendor commands (y/N)?")
        if resp.lower() in ['y', 'yes']:
            vendor_commands = self._ask_vendor_commands()

        self._update(custom_command, description, args, vendor_commands)
        self._save_to_file()
        print(color_string(f"Edited command {custom_command}", 'green'))