Debian Codegraph @

lib/ansible/utils/ssh_functions.py

raw
1 # (c) 2016, James Tanner
2 # (c) 2016, Toshio Kuratomi <[email protected]>
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 subprocess
22
23 from ansible.module_utils.common.text.converters import to_bytes
24 from ansible.utils.display import Display
25
26 display = Display()
27
28
29 _HAS_CONTROLPERSIST = {} # type: dict[str, bool]
30
31
32 def check_for_controlpersist(ssh_executable):
33 try:
34 # If we've already checked this executable
35 return _HAS_CONTROLPERSIST[ssh_executable]
36 except KeyError:
37 pass
38
39 b_ssh_exec = to_bytes(ssh_executable, errors='surrogate_or_strict')
40 has_cp = True
41 try:
42 cmd = subprocess.Popen([b_ssh_exec, '-o', 'ControlPersist'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
43 (out, err) = cmd.communicate()
44 if b"Bad configuration option" in err or b"Usage:" in err:
45 has_cp = False
46 except OSError:
47 has_cp = False
48
49 _HAS_CONTROLPERSIST[ssh_executable] = has_cp
50 return has_cp