switch module

class switch.ChParams(**kwargs)[source]

Bases: object

Set ConnectHandler parameters

user

User name

Type:

str

password
Type:

str

host

The hostname to connect to (can be an IP address, too).

Type:

str

device_type

e.g. “cisco_ios”. Check out netmiko’s documentation for your device type. You can find, for example, the module netmiko.cisco under which the submodules represent the string that you need. So there is netmiko.cisco.cisco_ios and “cisco_ios” is, what you would use.

Type:

str

conn_timeout

Connection timeout in seconds.

Type:

int

read_timeout

Timeout for reading in seconds.

Type:

float

class switch.CsSwitch(config)[source]

Bases: object

Connects to a switch and sends the commands configured in the configuration file.

config

pass

Type:

dict

params

All the parameters needed for netmiko’s ConnectHandler to connect to a switch. Populated with values from config for readabilty and it’s easier to write the needed keys that way.

Type:

ChParams

collect_commands() dict[source]

Creates a dictionary like

{
    "cmds_before": [],
    "cmds_after": [],
    "conf_cmds": []
}

from config.

If available, add the commands from files defined in the configuration with something like this:

  • cmds_before_file: /path/to/file

  • cmds_after_file: /path/to/file

  • conf_cmds_file: /path/to/file

save_config() None[source]

Since netmiko’s save_config method is not implemented for the class BaseConnection, it is necessary to define expect and answer strings in conquers.yaml to identify the string to look for in the output and answer accordingly. This is possible with netmiko:

Like so
output = device.send_command_timing(c, read_timeout=READTIMEOUT)
if "Are you sure" in output:
    output += device.send_command_timing(
        "Y",
        read_timeout=READTIMEOUT,
        strip_prompt=False,
        strip_command=False
    )

Note

send_cmds(cmd_set, type=None)[source]

Sends commands distinguishing between configuration commands and show commands. This function is used by switch.CsSwitch.send_cmds_ba() and switch.CsSwitch.send_conf_cmds().

Parameters:
  • cmd_set (list) –

    List of dictionaries containing commands. Is built by switch.CsSwitch.split_into_sets().

    Example for huawei changing the privilege level.
    [
        {
            'special': {
                'cmd': 'local-user admin privilege level 15'
                'expect': [
                    ('are your sure', 'Y')
                ]
            }
        }
    ]
    

  • type (str) – If type is None (default), configuration commands will be sent, show commands otherwise.

send_cmds_ba(ba=None) str[source]

Send show commands.

Parameters:

ba (str) – can be after or before

send_conf_cmds() str[source]

Send configuration commands.

split_into_sets(cmds) list[source]

Commands that require interaction are treated specially. Here they are recognized by their syntax which is explained in the configuration.

Regular commands are packed into lists which are sent altogether. Commands that require interaction are stuffed in between and executed differently making it possible to react to questions asked by the switch.

exception switch.CsSwitchError[source]

Bases: Exception