Ejemplo n.º 1
0
def get_reset_reasons(module):
    command = {
        "command": "show maintenance on-reload reset-reasons",
        "output": "text",
    }
    body = run_commands(module, [command])[0]
    return body
Ejemplo n.º 2
0
def get_udld_interface(module, interface):
    command = "show run udld all | section " + interface.title() + "$"
    interface_udld = {}
    mode = None
    mode_str = None
    try:
        body = run_commands(module, [{
            "command": command,
            "output": "text"
        }])[0]
        if "aggressive" in body:
            mode = "aggressive"
            mode_str = "aggressive"
        elif "no udld enable" in body:
            mode = "disabled"
            mode_str = "no udld enable"
        elif "no udld disable" in body:
            mode = "enabled"
            mode_str = "no udld disable"
        elif "udld disable" in body:
            mode = "disabled"
            mode_str = "udld disable"
        elif "udld enable" in body:
            mode = "enabled"
            mode_str = "udld enable"
        interface_udld["mode"] = mode

    except (KeyError, AttributeError, IndexError):
        interface_udld = {}

    return interface_udld, mode_str
Ejemplo n.º 3
0
def get_reset_reasons(module):
    command = {
        'command': 'show maintenance on-reload reset-reasons',
        'output': 'text'
    }
    body = run_commands(module, [command])[0]
    return body
Ejemplo n.º 4
0
def execute_show_commands(module, commands, output="text"):
    cmds = []
    for command in to_list(commands):
        cmd = {"command": command, "output": output}
        cmds.append(cmd)
    body = run_commands(module, cmds)
    return body
def execute_show_command(command, module, text=False):
    if text:
        cmds = [{"command": command, "output": "text"}]
    else:
        cmds = [{"command": command, "output": "json"}]

    return run_commands(module, cmds)
Ejemplo n.º 6
0
def get_ping_results(command, module):
    cmd = {"command": command, "output": "text"}
    ping = run_commands(module, [cmd])[0]

    if not ping:
        module.fail_json(
            msg="An unexpected error occurred. Check all params.",
            command=command,
            destination=module.params["dest"],
            vrf=module.params["vrf"],
            source=module.params["source"],
        )

    elif "can't bind to address" in ping:
        module.fail_json(msg="Can't bind to source address.", command=command)
    elif "bad context" in ping:
        module.fail_json(
            msg="Wrong VRF name inserted.",
            command=command,
            vrf=module.params["vrf"],
        )
    else:
        splitted_ping = ping.split("\n")
        reference_point = get_statistics_summary_line(splitted_ping)
        summary, ping_pass = get_summary(splitted_ping, reference_point)
        rtt = get_rtt(splitted_ping, summary["packet_loss"],
                      reference_point + 2)

    return (summary, rtt, ping_pass)
Ejemplo n.º 7
0
def execute_show_command(command, module, command_type="cli_show"):
    if command_type == "cli_show_ascii":
        cmds = [{"command": command, "output": "text"}]
    else:
        cmds = [{"command": command, "output": "json"}]

    return run_commands(module, cmds)
Ejemplo n.º 8
0
def interface_is_portchannel(name, module):
    """Checks to see if an interface is part of portchannel bundle
    Args:
        interface (str): full name of interface, i.e. Ethernet1/1
    Returns:
        True/False based on if interface is a member of a portchannel bundle
    """
    intf_type = get_interface_type(name)

    if intf_type == "ethernet":
        command = "show interface {0} | json".format(name)
        try:
            body = run_commands(module, [command])[0]
            interface_table = body["TABLE_interface"]["ROW_interface"]
        except (KeyError, AttributeError, IndexError):
            interface_table = None

        if interface_table:
            state = interface_table.get("eth_bundle")
            if state:
                return True
            else:
                return False

    return False
Ejemplo n.º 9
0
def get_switchport(port, module):
    """Gets current config of L2 switchport
    Args:
        device (Device): This is the device object of an NX-API enabled device
            using the Device class within device.py
        port (str): full name of interface, i.e. Ethernet1/1
    Returns:
        dictionary with k/v pairs for L2 vlan config
    """

    command = "show interface {0} switchport | json".format(port)

    try:
        body = run_commands(module, [command])[0]
        sp_table = body["TABLE_interface"]["ROW_interface"]
    except (KeyError, AttributeError, IndexError):
        sp_table = None

    if sp_table:
        key_map = {
            "interface": "name",
            "oper_mode": "mode",
            "switchport": "switchport",
            "access_vlan": "access_vlan",
            "access_vlan_name": "access_vlan_name",
            "native_vlan": "native_vlan",
            "native_vlan_name": "native_vlan_name",
            "trunk_vlans": "trunk_vlans",
        }
        sp = apply_key_map(key_map, sp_table)
        return sp

    else:
        return {}
Ejemplo n.º 10
0
def get_interface_mode(name, module):
    """Gets current mode of interface: layer2 or layer3
    Args:
        device (Device): This is the device object of an NX-API enabled device
            using the Device class within device.py
        interface (string): full name of interface, i.e. Ethernet1/1,
            loopback10, port-channel20, vlan20
    Returns:
        str: 'layer2' or 'layer3'
    """
    command = "show interface {0} | json".format(name)
    intf_type = get_interface_type(name)
    mode = "unknown"
    interface_table = {}

    try:
        body = run_commands(module, [command])[0]
        interface_table = body["TABLE_interface"]["ROW_interface"]
    except (KeyError, AttributeError, IndexError):
        return mode

    if interface_table:
        # HACK FOR NOW
        if intf_type in ["ethernet", "portchannel"]:
            mode = str(interface_table.get("eth_mode", "layer3"))
            if mode in ["access", "trunk"]:
                mode = "layer2"
            if mode == "routed":
                mode = "layer3"
        elif intf_type == "loopback" or intf_type == "svi":
            mode = "layer3"
    return mode
Ejemplo n.º 11
0
def execute_show_command(command, module):
    if "show run" not in command:
        command = {"command": command, "output": "json"}
    else:
        command = {"command": command, "output": "text"}

    return run_commands(module, [command])
def get_available_features(feature, module):
    available_features = {}
    feature_regex = r"(?P<feature>\S+)\s+\d+\s+(?P<state>.*)"
    command = {"command": "show feature", "output": "text"}

    try:
        body = run_commands(module, [command])[0]
        split_body = body.splitlines()
    except (KeyError, IndexError):
        return {}

    for line in split_body:
        try:
            match_feature = re.match(feature_regex, line, re.DOTALL)
            feature_group = match_feature.groupdict()
            feature = feature_group["feature"]
            state = feature_group["state"]
        except AttributeError:
            feature = ""
            state = ""

        if feature and state:
            if "enabled" in state:
                state = "enabled"

            if feature not in available_features:
                available_features[feature] = state
            else:
                if (available_features[feature] == "disabled"
                        and state == "enabled"):
                    available_features[feature] = state

    return available_features
Ejemplo n.º 13
0
def execute_show_command(command, module, output='json'):
    cmds = [{
        'command': command,
        'output': output,
    }]
    body = run_commands(module, cmds)
    return body
Ejemplo n.º 14
0
def execute_show_command(command, module):
    command = [{
        'command': command,
        'output': 'text',
    }]

    return run_commands(module, command)
Ejemplo n.º 15
0
def execute_show_command(command, module):
    if "show run" not in command:
        output = "json"
    else:
        output = "text"
    cmds = [{"command": command, "output": output}]
    body = run_commands(module, cmds)
    return body
Ejemplo n.º 16
0
def get_current(module):
    output = run_commands(
        module, {"command": "show running-config", "output": "text"}
    )
    return {
        "flush_routes": "ip igmp flush-routes" in output[0],
        "enforce_rtr_alert": "ip igmp enforce-router-alert" in output[0],
    }
Ejemplo n.º 17
0
def execute_show_command(command, module, command_type='cli_show'):
    output = 'text'
    commands = [{
        'command': command,
        'output': output,
    }]
    out = run_commands(module, commands)
    return out
Ejemplo n.º 18
0
def get_system_mode(module):
    command = {"command": "show system mode", "output": "text"}
    body = run_commands(module, [command])[0]
    if body and "normal" in body.lower():
        mode = "normal"
    else:
        mode = "maintenance"
    return mode
Ejemplo n.º 19
0
def execute_show_command(command, module, command_type="cli_show"):
    if "show run" not in command:
        output = "json"
    else:
        output = "text"

    commands = [{"command": command, "output": output}]
    return run_commands(module, commands)
Ejemplo n.º 20
0
def execute_show_command(module, command):
    format = 'text'
    cmds = [{
        'command': command,
        'output': format,
    }]
    output = run_commands(module, cmds)
    return output
Ejemplo n.º 21
0
def get_system_mode(module):
    command = {'command': 'show system mode', 'output': 'text'}
    body = run_commands(module, [command])[0]
    if body and 'normal' in body.lower():
        mode = 'normal'
    else:
        mode = 'maintenance'
    return mode
def execute_show_command(command, module, text=False):
    command = {
        'command': command,
        'output': 'json',
    }
    if text:
        command['output'] = 'text'

    return run_commands(module, command)
Ejemplo n.º 23
0
def get_current(module):
    output = run_commands(module, {
        'command': 'show running-config',
        'output': 'text'
    })
    return {
        'flush_routes': 'ip igmp flush-routes' in output[0],
        'enforce_rtr_alert': 'ip igmp enforce-router-alert' in output[0]
    }
Ejemplo n.º 24
0
def execute_show_commands(module, commands, output='text'):
    cmds = []
    for command in to_list(commands):
        cmd = {'command': command,
               'output': output,
               }
        cmds.append(cmd)
    body = run_commands(module, cmds)
    return body
Ejemplo n.º 25
0
def execute_show_command(command, module):
    if 'show run' not in command:
        output = 'json'
    else:
        output = 'text'
    cmds = [{
        'command': command,
        'output': output,
    }]
    return run_commands(module, cmds)[0]
Ejemplo n.º 26
0
def get_active_vpc_peer_link(module):
    peer_link = None

    try:
        body = run_commands(module, ['show vpc brief | json'])[0]
        peer_link = body['TABLE_peerlink']['ROW_peerlink']['peerlink-ifindex']
    except (KeyError, AttributeError, TypeError):
        return peer_link

    return peer_link
Ejemplo n.º 27
0
def get_active_vpc_peer_link(module):
    peer_link = None

    try:
        body = run_commands(module, ["show vpc brief | json"])[0]
        peer_link = body["TABLE_peerlink"]["ROW_peerlink"]["peerlink-ifindex"]
    except (KeyError, AttributeError, TypeError):
        return peer_link

    return peer_link
 def run(self, command, output="text"):
     command_string = command
     command = {"command": command, "output": output}
     resp = run_commands(self.module, [command], check_rc="retry_json")
     try:
         return resp[0]
     except IndexError:
         self.warnings.append(
             "command %s failed, facts for this command will not be populated"
             % command_string)
         return None
Ejemplo n.º 29
0
def execute_show_command(command, module):
    if "show run" not in command:
        output = "json"
    else:
        output = "text"
    cmds = [{"command": command, "output": output}]
    body = run_commands(module, cmds, check_rc=False)
    if body and "Invalid" in body[0]:
        return []
    else:
        return body
Ejemplo n.º 30
0
def execute_show_command(command, module):
    iteration = 0
    cmds = [{"command": command, "output": "text"}]

    while iteration < 10:
        body = run_commands(module, cmds)[0]
        if body:
            return body
        else:
            time.sleep(2)
            iteration += 1