def check_args(self):
        """ Check args """

        if self.acl_name:
            if self.acl_name.isdigit():
                if int(self.acl_name) < 2000 or int(self.acl_name) > 4999:
                    self.module.fail_json(
                        msg=
                        'Error: The value of acl_name is out of [2000 - 4999].'
                    )
            else:
                if len(self.acl_name) < 1 or len(self.acl_name) > 32:
                    self.module.fail_json(
                        msg='Error: The len of acl_name is out of [1 - 32].')

        if self.interface:
            cmd = "display current-configuration | ignore-case section include interface %s" % self.interface
            rc, out, err = exec_command(self.module, cmd)
            if rc != 0:
                self.module.fail_json(msg=err)
            result = str(out).strip()
            if result:
                tmp = result.split('\n')
                if "display" in tmp[0]:
                    tmp.pop(0)
                if not tmp:
                    self.module.fail_json(
                        msg='Error: The interface %s is not in the device.' %
                        self.interface)
    def get_current_config(self):
        """get current configuration"""

        cmd = "display current-configuration | section include bgp %s" % self.bgp_instance
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        return out
    def get_evpn_overlay_config(self):
        """get evpn-overlay enable configuration"""

        cmd = "display current-configuration | include ^evpn-overlay enable"
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        return out
Beispiel #4
0
    def get_netstream_config(self):
        """get current netstream configuration"""

        cmd = "display current-configuration | include ^netstream export"
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        config = str(out).strip()
        return config
Beispiel #5
0
    def get_evpn_global_info(self):
        """ get current EVPN global configuration"""

        self.global_info['evpnOverLay'] = 'disable'
        cmd = "display current-configuration | include ^evpn-overlay enable"
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        if out:
            self.global_info['evpnOverLay'] = 'enable'
    def load_config(self, config):
        """Sends configuration commands to the remote device"""

        rc, out, err = exec_command(self.module, 'mmi-mode enable')
        if rc != 0:
            self.module.fail_json(msg='unable to set mmi-mode enable',
                                  output=err)
        rc, out, err = exec_command(self.module, 'system-view immediately')
        if rc != 0:
            self.module.fail_json(msg='unable to enter system-view',
                                  output=err)

        for cmd in config:
            rc, out, err = exec_command(self.module, cmd)
            if rc != 0:
                if "unrecognized command found" in err.lower():
                    self.module.fail_json(
                        msg=
                        "Error:The parameter is incorrect or the interface does not support this parameter."
                    )
                else:
                    self.module.fail_json(msg=cli_err_msg(cmd.strip(), err))

        exec_command(self.module, 'return')
    def get_config(self, flags=None):
        """Retrieves the current config from the device or cache
        """
        flags = [] if flags is None else flags

        cmd = 'display current-configuration '
        cmd += ' '.join(flags)
        cmd = cmd.strip()

        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        cfg = str(out).strip()

        return cfg
    def get_end_state(self):
        """ Get config end state """

        cmd = "display current-configuration | ignore-case section include interface %s | include traffic-filter" % self.interface
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        result = str(out).strip()
        end = []
        if result:
            tmp = result.split('\n')
            if "display" in tmp[0]:
                tmp.pop(0)
            for item in tmp:
                end.append(item.strip())
            self.end_state["acl interface"] = end
    def get_config_in_bgp_view(self):
        """Get configuration in BGP view"""

        cmd = "display current-configuration | section include"
        if self.as_number:
            if self.bgp_instance:
                cmd += " bgp %s instance %s" % (self.as_number,
                                                self.bgp_instance)
            else:
                cmd += " bgp %s" % self.as_number
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        config = out.strip() if out else ""
        if cmd == config:
            return ''

        return config
Beispiel #10
0
    def get_exist_timer_out_para(self):
        """Get exist netstream timeout parameters"""

        active_tmp = dict()
        inactive_tmp = dict()
        tcp_tmp = dict()
        active_tmp["ip"] = "30"
        active_tmp["vxlan"] = "30"
        inactive_tmp["ip"] = "30"
        inactive_tmp["vxlan"] = "30"
        tcp_tmp["ip"] = "absent"
        tcp_tmp["vxlan"] = "absent"

        cmd = "display current-configuration | include ^netstream timeout"
        rc, out, err = exec_command(self.module, cmd)
        if rc != 0:
            self.module.fail_json(msg=err)
        config = str(out).strip()
        if config:
            config = config.lstrip()
            config_list = config.split('\n')
            for config_mem in config_list:
                config_mem = config_mem.lstrip()
                config_mem_list = config_mem.split(' ')
                if len(config_mem_list) > 4 and config_mem_list[2] == "ip":
                    if config_mem_list[3] == "active":
                        active_tmp["ip"] = config_mem_list[4]
                    if config_mem_list[3] == "inactive":
                        inactive_tmp["ip"] = config_mem_list[4]
                    if config_mem_list[3] == "tcp-session":
                        tcp_tmp["ip"] = "present"
                if len(config_mem_list) > 4 and config_mem_list[2] == "vxlan":
                    if config_mem_list[4] == "active":
                        active_tmp["vxlan"] = config_mem_list[5]
                    if config_mem_list[4] == "inactive":
                        inactive_tmp["vxlan"] = config_mem_list[5]
                    if config_mem_list[4] == "tcp-session":
                        tcp_tmp["vxlan"] = "present"
        self.existing["active_timeout"].append(active_tmp)
        self.existing["inactive_timeout"].append(inactive_tmp)
        self.existing["tcp_timeout"].append(tcp_tmp)
def get_config(module, flags):
    """Retrieves the current config from the device or cache"""

    flags = [] if flags is None else flags

    cmd = 'display current-configuration '
    cmd += ' '.join(flags)
    cmd = cmd.strip()

    rc, out, err = exec_command(module, cmd)
    if rc != 0:
        module.fail_json(msg=err)
    config = str(out).strip()
    if config.startswith("display"):
        configs = config.split("\n")
        if len(configs) > 1:
            return "\n".join(configs[1:])
        else:
            return ""
    else:
        return config
def _load_config(module, config):
    """Sends configuration commands to the remote device
    """
    connection = Connection(module._socket_path)
    rc, out, err = exec_command(module, 'mmi-mode enable')
    if rc != 0:
        module.fail_json(msg='unable to set mmi-mode enable', output=err)
    rc, out, err = exec_command(module, 'system-view immediately')
    if rc != 0:
        module.fail_json(msg='unable to enter system-view', output=err)
    current_view_prompt = system_view_prompt = connection.get_prompt()

    for index, cmd in enumerate(config):
        level = command_level(cmd)
        current_view_prompt = connection.get_prompt()
        rc, out, err = exec_command(module, cmd)
        if rc != 0:
            print_msg = cli_err_msg(cmd.strip(), err)
            # re-try command max 3 times
            for i in (1, 2, 3):
                current_view_prompt = connection.get_prompt()
                if current_view_prompt != system_view_prompt and not_user_view(
                        current_view_prompt):
                    exec_command(module, "quit")
                    current_view_prompt = connection.get_prompt()
                    # if current view is system-view, break.
                    if current_view_prompt == system_view_prompt and level > 0:
                        break
                elif current_view_prompt == system_view_prompt or not not_user_view(
                        current_view_prompt):
                    break
                rc, out, err = exec_command(module, cmd)
                if rc == 0:
                    print_msg = None
                    break
            if print_msg is not None:
                module.fail_json(msg=print_msg)