| 1 |
# |
| 2 |
# (c) 2016 Red Hat Inc. |
| 3 |
# |
| 4 |
# This file is part of Ansible |
| 5 |
# |
| 6 |
# Ansible is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# Ansible is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
# |
| 19 |
from __future__ import annotations |
| 20 |
|
| 21 |
import re |
| 22 |
|
| 23 |
from abc import ABC, abstractmethod |
| 24 |
|
| 25 |
from ansible.errors import AnsibleConnectionFailure |
| 26 |
|
| 27 |
|
| 28 |
class TerminalBase(ABC): |
| 29 |
""" |
| 30 |
A base class for implementing cli connections |
| 31 |
|
| 32 |
.. note:: Unlike most of Ansible, nearly all strings in |
| 33 |
:class:`TerminalBase` plugins are byte strings. This is because of |
| 34 |
how close to the underlying platform these plugins operate. Remember |
| 35 |
to mark literal strings as byte string (``b"string"``) and to use |
| 36 |
:func:`~ansible.module_utils.common.text.converters.to_bytes` and |
| 37 |
:func:`~ansible.module_utils.common.text.converters.to_text` to avoid unexpected |
| 38 |
problems. |
| 39 |
""" |
| 40 |
|
| 41 |
#: compiled bytes regular expressions as stdout |
| 42 |
terminal_stdout_re = [] # type: list[re.Pattern] |
| 43 |
|
| 44 |
#: compiled bytes regular expressions as stderr |
| 45 |
terminal_stderr_re = [] # type: list[re.Pattern] |
| 46 |
|
| 47 |
#: compiled bytes regular expressions to remove ANSI codes |
| 48 |
ansi_re = [ |
| 49 |
re.compile(br'\x1b\[\?1h\x1b='), # CSI ? 1 h ESC = |
| 50 |
re.compile(br'\x08.'), # [Backspace] . |
| 51 |
re.compile(br"\x1b\[m"), # ANSI reset code |
| 52 |
] |
| 53 |
|
| 54 |
#: terminal initial prompt |
| 55 |
terminal_initial_prompt = None |
| 56 |
|
| 57 |
#: terminal initial answer |
| 58 |
terminal_initial_answer = None |
| 59 |
|
| 60 |
#: Send newline after prompt match |
| 61 |
terminal_inital_prompt_newline = True |
| 62 |
|
| 63 |
def __init__(self, connection): |
| 64 |
self._connection = connection |
| 65 |
|
| 66 |
def _exec_cli_command(self, cmd, check_rc=True): |
| 67 |
""" |
| 68 |
Executes the CLI command on the remote device and returns the output |
| 69 |
|
| 70 |
:arg cmd: Byte string command to be executed |
| 71 |
""" |
| 72 |
return self._connection.exec_command(cmd) |
| 73 |
|
| 74 |
def _get_prompt(self): |
| 75 |
""" |
| 76 |
Returns the current prompt from the device |
| 77 |
|
| 78 |
:returns: A byte string of the prompt |
| 79 |
""" |
| 80 |
return self._connection.get_prompt() |
| 81 |
|
| 82 |
def on_open_shell(self): |
| 83 |
"""Called after the SSH session is established |
| 84 |
|
| 85 |
This method is called right after the invoke_shell() is called from |
| 86 |
the Paramiko SSHClient instance. It provides an opportunity to setup |
| 87 |
terminal parameters such as disabling paging for instance. |
| 88 |
""" |
| 89 |
pass |
| 90 |
|
| 91 |
def on_close_shell(self): |
| 92 |
"""Called before the connection is closed |
| 93 |
|
| 94 |
This method gets called once the connection close has been requested |
| 95 |
but before the connection is actually closed. It provides an |
| 96 |
opportunity to clean up any terminal resources before the shell is |
| 97 |
actually closed |
| 98 |
""" |
| 99 |
pass |
| 100 |
|
| 101 |
def on_become(self, passwd=None): |
| 102 |
"""Called when privilege escalation is requested |
| 103 |
|
| 104 |
:kwarg passwd: String containing the password |
| 105 |
|
| 106 |
This method is called when the privilege is requested to be elevated |
| 107 |
in the play context by setting become to True. It is the responsibility |
| 108 |
of the terminal plugin to actually do the privilege escalation such |
| 109 |
as entering `enable` mode for instance |
| 110 |
""" |
| 111 |
pass |
| 112 |
|
| 113 |
def on_unbecome(self): |
| 114 |
"""Called when privilege deescalation is requested |
| 115 |
|
| 116 |
This method is called when the privilege changed from escalated |
| 117 |
(become=True) to non escalated (become=False). It is the responsibility |
| 118 |
of this method to actually perform the deauthorization procedure |
| 119 |
""" |
| 120 |
pass |
| 121 |
|
| 122 |
def on_authorize(self, passwd=None): |
| 123 |
"""Deprecated method for privilege escalation |
| 124 |
|
| 125 |
:kwarg passwd: String containing the password |
| 126 |
""" |
| 127 |
return self.on_become(passwd) |
| 128 |
|
| 129 |
def on_deauthorize(self): |
| 130 |
"""Deprecated method for privilege deescalation |
| 131 |
""" |
| 132 |
return self.on_unbecome() |