Example #1
0
    def __login_over_telnet(self, device):
        """ Creates an Telnet client object, then tries to login to the device using its attributes.

            Args:
                device (:obj: `Device`): The device that the manager will connect to.

        """
        try:
            self.__obj_connect = Telnet(device.ip_address, "23", 5)
            # self.obj_connect.set_debuglevel(1) # uncomment this line to enable debug for Telnet obj.
        except Exception as error:
            print(f"[ ! ] {error}.")
            auxiliar_functions.close()
        else:
            if device.vty_username != "":
                self.__obj_connect.read_until(b"Username:"******"\n")
                sleep(0.5)
            self.__obj_connect.read_until(b"Password:"******"\n")
            sleep(0.5)
            self.__identify_errors(device)
            self.__obj_connect.write(b"enable\n")
            self.__obj_connect.read_until(b"Password:"******"\n")
            sleep(0.5)
            self.__identify_errors(device)
Example #2
0
    def __configure(self, device, configurations):
        """ Start running commands on device if the configurations keys exists on 'config.json' file.

            Args:
                device (:obj: `Device`): The device to be configured.
                configurations (str): The command keys based on 'config.json'.

        """
        for config_key in configurations:
            if config_key in self.configuration_keywords:
                if config_key == 'SET_HOSTNAME':
                    variables["DEVICE_HOSTNAME"] = input("[->] Set hostname: ")
                    self.__send_commands(device, config_key)
                elif config_key == 'CREATE_VLAN' or config_key == 'DELETE_VLAN':
                    if self.__vlans_to_configure == None:
                        print(
                            "\n\n[ ! ] You must specify the VLANs to be configured!(see README or documentation.)"
                        )
                        auxiliar_functions.close()
                    for vlan in self.__vlans_to_configure:
                        if int(vlan) not in range(3968,
                                                  4048) and int(vlan) != 4094:
                            self.variables["VLAN_NUMBER"] = vlan
                            self.__send_commands(device, config_key)
                    self.variables["VLAN_NUMBER"] = self.vlans_to_configure[0]
                else:
                    self.__send_commands(device, config_key)
            else:
                print(
                    f"\n\n[ ! ] There is no valid configuration for '{config_key}'."
                )
                auxiliar_functions.close()
Example #3
0
    def __login_over_ssh(self, device):
        """ Creates an SSHClient object, load the keys, then tries to login to the device using its attributes.

            Args:
                device (:obj: `Device`): The device that the manager will connect to.

        """
        self.__obj_connect = SSHClient()
        self.__obj_connect.load_system_host_keys()
        self.__obj_connect.set_missing_host_key_policy(AutoAddPolicy())
        try:
            self.__obj_connect.connect(device.ip_address, 22,
                                       device.vty_username,
                                       device.vty_password)
            self.__shell = self.__obj_connect.invoke_shell(
            )  # Opens a shell to run commands.
        except Exception as error:
            print(f"[ ! ] {error}")
            self.__obj_connect.close()
            auxiliar_functions.close()
        else:
            self.__identify_errors(device)
            self.__shell.send(b"enable\n")
            sleep(0.5)
            self.__shell.send(device.enable_secret.encode() + b"\n")
            sleep(0.5)
            self.__identify_errors(device)
Example #4
0
 def configure_devices(self, *args):
     """ Starts the configuration of devices on the devices list. """
     for _, device in enumerate(self.__devices):
         self.variables['ENABLE_PASSWORD'] = device.enable_secret
         self.variables['DOMAIN_NAME'] = device.domain_name
         auxiliar_functions.clear()
         print(f"\n[ + ] CONFIGURING  THE {_+1}ยบ DEVICE...\n")
         if len(args) < 1:
             print(
                 f"\n\n[ ! ] You must choose at least one configuration (see README or documentation)."
             )
             auxiliar_functions.close()
         if device.connection_protocol == "TELNET":
             try:
                 self.__login_over_telnet(device)
                 self.__configure(device, args)
                 self.__obj_connnect.close()
             except Exception as e:
                 print(f"\n\n{e}")
                 auxiliar_functions.close()
         else:
             try:
                 self.__login_over_ssh(device)
                 self.__configure(device, args)
                 self.__obj_connect.close()
             except Exception as e:
                 print(f"\n\n{e}")
                 auxiliar_functions.close()
     auxiliar_functions.close()
Example #5
0
        def find_error_on_line(output_line):
            """ Verify if the output command line has errors based on a predefined dict.

                Args:
                    output_line (str): The device output command line to be verified.

            """
            found_error = list(
                filter(lambda error: error in output_line, errors_keywords))
            if len(found_error) > 0:
                print(f"[ ! ] {errors_dict[found_error[0]]}")
                auxiliar_functions.close()
            else:
                print(output_line, end='')