Skip to content

Commit

Permalink
add scrapli
Browse files Browse the repository at this point in the history
  • Loading branch information
natenka committed Apr 5, 2021
1 parent 55823a0 commit 98c8fdf
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
7 changes: 6 additions & 1 deletion docs/source/book/18_ssh_telnet/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ Python has several modules that allow you to connect to network devices and exec

* ``netmiko`` is a "wrapper" which is oriented to work with network devices

This section covers all four modules and describes how to connect to several
* ``scrapli`` - is a module that allows you to connect to network equipment
using Telnet, SSH or NETCONF


This section covers all five modules and describes how to connect to several
devices in parallel. Three routers are used in section examples. There are no
requirements for them, only configured SSHv2 and Telnet.

Expand All @@ -54,5 +58,6 @@ Parameters used in these section:
telnetlib
paramiko
netmiko
scrapli
further_reading
../../exercises/18_exercises
56 changes: 31 additions & 25 deletions docs/source/book/18_ssh_telnet/scrapli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -357,25 +357,25 @@ without error and ``True`` if it failed.
send_config method
~~~~~~~~~~~~~~~~~~

Метод ``send_config`` позволяет отправить одну команду конфигурационного режима.
The ``send_config`` method allows you to send one configuration mode command.

Пример использования:
Example:

.. code:: python
In [33]: r = ssh.send_config("username user1 password password1")
Так как scrapli удаляет команду из вывода, по умолчанию, при использовании
send_config, в атрибуте result будет пустая строка (если не было ошибки при
выполнении команды):
Since scrapli removes the command from the output, by default, when using
``send_config``, the result attribute will contain an empty string (if there was
no error while executing the command):

.. code:: python
In [34]: r.result
Out[34]: ''
Можно добавлять параметр ``strip_prompt=False`` и тогда в выводе появится
приглашение:
You can add the parameter ``strip_prompt=False`` and then the prompt will
appear in the output:

.. code:: python
Expand All @@ -385,13 +385,13 @@ send_config, в атрибуте result будет пустая строка (е
Out[38]: 'R1(config)#'
Методы send_commands, send_configs
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
send_commands, send_configs
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Методы send_commands, send_configs отличаются от send_command, send_config тем,
что могут отправлять несколько команд.
Кроме того, эти методы возвращают не Response, а MultiResponse, который можно
в целом воспринимать как список Response, по одному для каждой команды.
The send_commands, send_configs methods differ from send_command, send_config
in that they can send several commands. In addition, these methods do not return
a Response, but a MultiResponse object, which can generally be thought of as a list
of Response objects, one for each command.

.. code:: python
Expand Down Expand Up @@ -425,10 +425,10 @@ send_config, в атрибуте result будет пустая строка (е
In [50]: reply[0].result
Out[50]: '*08:38:20.115 UTC Thu Apr 1 2021'
При отправке нескольких команд также очень удобно использовать параметр
``stop_on_failed``. По умолчанию он равен False, поэтому выполняются все
команды, но если указать ``stop_on_failed=True``, после возникновения
ошибки в какой-то команде, следующие команды не будут выполняться:
When sending multiple commands, it is also very convenient to use
the ``stop_on_failed`` parameter. By default, it is False, so all commands
are executed, but if you specify ``stop_on_failed=True``, after an error
occurs in some command, the following commands will not be executed:

.. code:: python
Expand All @@ -454,12 +454,12 @@ send_config, в атрибуте result будет пустая строка (е
% Invalid input detected at '^' marker.
Подключение telnet
Telnet connection
~~~~~~~~~~~~~~~~~~

Для подключения к оборудовани по Telnet надо указать transport равным
telnet и обязательно указать параметр port равным 23 (или тому порту который
используется у вас для подключения по Telnet):
To connect to equipment via Telnet, you must specify transport equal to ``telnet``
and be sure to specify the port parameter equal to 23 (or the port that you use
to connect via Telnet):

.. code:: python
Expand All @@ -474,7 +474,7 @@ telnet и обязательно указать параметр port равны
"auth_secondary": "cisco",
"auth_strict_key": False,
"transport": "telnet",
"port": 23, # обязательно указывать при подключении telnet
"port": 23, # must be specified when connecting telnet
}
Expand All @@ -494,8 +494,10 @@ telnet и обязательно указать параметр port равны
print(output)
Примеры использования scrapli
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Scrapli examples
~~~~~~~~~~~~~~~~

Basic example of sending show command:

.. code:: python
Expand Down Expand Up @@ -527,6 +529,7 @@ telnet и обязательно указать параметр port равны
output = send_show(r1, "sh ip int br")
print(output)
Basic example of sending config commands:

.. code:: python
Expand Down Expand Up @@ -559,6 +562,7 @@ telnet и обязательно указать параметр port равны
output = send_show(r1, ["sh ip int br", "sh ver | i uptime"])
pprint(output, width=120)
An example of sending configuration commands with error checking:

.. code:: python
Expand All @@ -583,7 +587,9 @@ telnet и обязательно указать параметр port равны
reply = ssh.send_configs(cfg_commands, stop_on_failed=strict)
for cmd_reply in reply:
if cmd_reply.failed:
print(f"При выполнении команды возникла ошибка:\n{reply.result}\n")
print(
f"An error occurred while executing the command::\n{reply.result}\n"
)
output = reply.result
return output
Expand Down

0 comments on commit 98c8fdf

Please sign in to comment.