/
_ssh_client_base.py
1495 lines (1296 loc) · 57.6 KB
/
_ssh_client_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2018 - 2019 Alexey Stepanov aka penguinolog.
# Copyright 2013 - 2016 Mirantis, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
"""SSH client helper based on Paramiko. Base class."""
__all__ = ("SSHClientBase", "SshExecuteAsyncResult")
# Standard Library
import concurrent.futures
import copy
import datetime
import getpass
import logging
import shlex
import socket
import stat
import time
import typing
import warnings
# External Dependencies
import paramiko # type: ignore
import tenacity # type: ignore
import threaded
# Package Implementation
from exec_helpers import _log_templates
from exec_helpers import _ssh_helpers
from exec_helpers import api
from exec_helpers import constants
from exec_helpers import exceptions
from exec_helpers import exec_result
from exec_helpers import proc_enums
from exec_helpers import ssh_auth
from exec_helpers.api import CalledProcessErrorSubClassT
from exec_helpers.api import OptionalStdinT
from exec_helpers.api import OptionalTimeoutT
from exec_helpers.proc_enums import ExitCodeT
# Local Implementation
from ._ssh_helpers import HostsSSHConfigs
from ._ssh_helpers import SSHConfig
_OptionalSSHAuthMapT = typing.Optional[typing.Union[typing.Dict[str, ssh_auth.SSHAuth], ssh_auth.SSHAuthMapping]]
_OptionalSSHConfigArgT = typing.Union[
str,
paramiko.SSHConfig,
typing.Dict[str, typing.Dict[str, typing.Union[str, int, bool, typing.List[str]]]],
HostsSSHConfigs,
None,
]
_SSHConnChainT = typing.List[typing.Tuple[SSHConfig, ssh_auth.SSHAuth]]
_OptSSHAuthT = typing.Optional[ssh_auth.SSHAuth]
class RetryOnExceptions(tenacity.retry_if_exception): # type: ignore
"""Advanced retry on exceptions."""
def __init__(
self,
retry_on: "typing.Union[typing.Type[BaseException], typing.Tuple[typing.Type[BaseException], ...]]",
reraise: "typing.Union[typing.Type[BaseException], typing.Tuple[typing.Type[BaseException], ...]]",
) -> None:
"""Retry on exceptions, except several types.
:param retry_on: Exceptions to retry on
:param reraise: Exceptions, which should be reraised, even if subclasses retry_on
"""
super().__init__(lambda e: isinstance(e, retry_on) and not isinstance(e, reraise))
# noinspection PyTypeHints
class SshExecuteAsyncResult(api.ExecuteAsyncResult):
"""Override original NamedTuple with proper typing."""
@property
def interface(self) -> paramiko.Channel:
"""Override original NamedTuple with proper typing."""
return super().interface
@property
def stdin(self) -> paramiko.ChannelFile: # type: ignore
"""Override original NamedTuple with proper typing."""
return super().stdin
@property
def stderr(self) -> typing.Optional[paramiko.ChannelFile]: # type: ignore
"""Override original NamedTuple with proper typing."""
return super().stderr
@property
def stdout(self) -> typing.Optional[paramiko.ChannelFile]: # type: ignore
"""Override original NamedTuple with proper typing."""
return super().stdout
class _SudoContext(typing.ContextManager[None]):
"""Context manager for call commands with sudo."""
__slots__ = ("__ssh", "__sudo_status", "__enforce")
def __init__(self, ssh: "SSHClientBase", enforce: typing.Optional[bool] = None) -> None:
"""Context manager for call commands with sudo.
:param ssh: connection instance
:type ssh: SSHClientBase
:param enforce: sudo mode for context manager
:type enforce: typing.Optional[bool]
"""
self.__ssh: "SSHClientBase" = ssh
self.__sudo_status: bool = ssh.sudo_mode
self.__enforce: typing.Optional[bool] = enforce
def __enter__(self) -> None:
self.__sudo_status = self.__ssh.sudo_mode
if self.__enforce is not None:
self.__ssh.sudo_mode = self.__enforce
def __exit__(self, exc_type: typing.Any, exc_val: typing.Any, exc_tb: typing.Any) -> None:
self.__ssh.sudo_mode = self.__sudo_status
class _KeepAliveContext(typing.ContextManager[None]):
"""Context manager for keepalive management."""
__slots__ = ("__ssh", "__keepalive_status", "__enforce")
def __init__(self, ssh: "SSHClientBase", enforce: int) -> None:
"""Context manager for keepalive management.
:param ssh: connection instance
:type ssh: SSHClientBase
:param enforce: keepalive period for context manager
:type enforce: int
"""
self.__ssh: "SSHClientBase" = ssh
self.__keepalive_status: int = ssh.keepalive_period
self.__enforce: int = enforce
def __enter__(self) -> None:
self.__ssh.__enter__()
self.__keepalive_status = self.__ssh.keepalive_period
self.__ssh.keepalive_mode = self.__enforce
def __exit__(self, exc_type: typing.Any, exc_val: typing.Any, exc_tb: typing.Any) -> None:
# Exit before releasing!
self.__ssh.__exit__(exc_type=exc_type, exc_val=exc_val, exc_tb=exc_tb) # type: ignore
self.__ssh.keepalive_mode = self.__keepalive_status
class SSHClientBase(api.ExecHelper):
"""SSH Client helper."""
__slots__ = (
"__hostname",
"__port",
"__auth_mapping",
"__ssh",
"__sftp",
"__sudo_mode",
"__keepalive_period",
"__verbose",
"__ssh_config",
"__sock",
"__conn_chain",
)
def __hash__(self) -> int:
"""Hash for usage as dict keys."""
return hash((self.__class__, self.hostname, self.port, self.auth))
def __init__(
self,
host: str,
port: typing.Optional[int] = None,
username: typing.Optional[str] = None,
password: typing.Optional[str] = None,
*,
private_keys: typing.Optional[typing.Sequence[paramiko.RSAKey]] = None,
auth: _OptSSHAuthT = None,
verbose: bool = True,
ssh_config: _OptionalSSHConfigArgT = None,
ssh_auth_map: _OptionalSSHAuthMapT = None,
sock: typing.Optional[typing.Union[paramiko.ProxyCommand, paramiko.Channel, socket.socket]] = None,
keepalive: typing.Union[int, bool] = 1,
) -> None:
"""Main SSH Client helper.
:param host: remote hostname
:type host: str
:param port: remote ssh port
:type port: typing.Optional[int]
:param username: remote username.
:type username: typing.Optional[str]
:param password: remote password
:type password: typing.Optional[str]
:param private_keys: private keys for connection
:type private_keys: typing.Optional[typing.Sequence[paramiko.RSAKey]]
:param auth: credentials for connection
:type auth: typing.Optional[ssh_auth.SSHAuth]
:param verbose: show additional error/warning messages
:type verbose: bool
:param ssh_config: SSH configuration for connection. Maybe config path, parsed as dict and paramiko parsed.
:type ssh_config:
typing.Union[
str,
paramiko.SSHConfig,
typing.Dict[str, typing.Dict[str, typing.Union[str, int, bool, typing.List[str]]]],
HostsSSHConfigs,
None
]
:param ssh_auth_map: SSH authentication information mapped to host names. Useful for complex SSH Proxy cases.
:type ssh_auth_map: typing.Optional[typing.Union[typing.Dict[str, ssh_auth.SSHAuth], ssh_auth.SSHAuthMapping]]
:param sock: socket for connection. Useful for ssh proxies support
:type sock: typing.Optional[typing.Union[paramiko.ProxyCommand, paramiko.Channel, socket.socket]]
:param keepalive: keepalive period
:type keepalive: typing.Union[int, bool]
.. note:: auth has priority over username/password/private_keys
.. note::
for proxy connection auth information is collected from SSHConfig
if ssh_auth_map record is not available
.. versionchanged:: 6.0.0 private_keys, auth and verbose became keyword-only arguments
.. versionchanged:: 6.0.0 added optional ssh_config for ssh-proxy & low level connection parameters handling
.. versionchanged:: 6.0.0 added optional ssh_auth_map for ssh proxy cases with authentication on each step
.. versionchanged:: 6.0.0 added optional sock for manual proxy chain handling
.. versionchanged:: 6.0.0 keepalive exposed to constructor
.. versionchanged:: 6.0.0 keepalive became int, now used in ssh transport as period of keepalive requests
.. versionchanged:: 6.0.0 private_keys is deprecated
"""
if private_keys is not None:
warnings.warn(
"private_keys setting without SSHAuth object is deprecated and will be removed at short time.",
DeprecationWarning,
)
# Init ssh config. It's main source for connection parameters
if isinstance(ssh_config, HostsSSHConfigs):
self.__ssh_config: HostsSSHConfigs = ssh_config
else:
self.__ssh_config = _ssh_helpers.parse_ssh_config(ssh_config, host)
# Get config. We are not resolving full chain. If you are have a chain by some reason - init config manually.
config: SSHConfig = self.__ssh_config[host]
# Save resolved hostname and port
self.__hostname: str = config.hostname
if port is not None:
self.__port: int = port
else:
self.__port = config.port if config.port is not None else 22
# Store initial auth mapping
self.__auth_mapping = ssh_auth.SSHAuthMapping(ssh_auth_map)
# We are already resolved hostname
if self.hostname not in self.__auth_mapping and host in self.__auth_mapping:
self.__auth_mapping[self.hostname] = self.__auth_mapping[host]
self.__sudo_mode = False
self.__keepalive_period: int = int(keepalive)
self.__verbose: bool = verbose
self.__sock = sock
self.__ssh: paramiko.SSHClient
self.__sftp: typing.Optional[paramiko.SFTPClient] = None
# Rebuild SSHAuth object if required.
# Priority: auth > credentials > auth mapping
if auth is not None:
self.__auth_mapping[self.hostname] = real_auth = copy.copy(auth)
elif self.hostname not in self.__auth_mapping or any((username, password, private_keys)):
self.__auth_mapping[self.hostname] = real_auth = ssh_auth.SSHAuth(
username=username if username is not None else config.user,
password=password,
keys=private_keys,
key_filename=config.identityfile,
)
else:
real_auth = self.__auth_mapping[self.hostname]
# Init super with host and real port and username
mod_name = "exec_helpers" if self.__module__.startswith("exec_helpers") else self.__module__
log_username: str = real_auth.username if real_auth.username is not None else getpass.getuser()
super().__init__(
logger=logging.getLogger(f"{mod_name}.{self.__class__.__name__}").getChild(
f"({log_username}@{host}:{self.port})"
)
)
# Update config for target host: merge with data from credentials and parameters.
# SSHConfig is the single source for hostname/port/... during low level connection construction.
self.__rebuild_ssh_config()
# Build connection chain once and use it for connection later
if sock is None:
self.__conn_chain: _SSHConnChainT = self.__build_connection_chain()
else:
self.__conn_chain = []
self.__connect()
def __rebuild_ssh_config(self) -> None:
"""Rebuild main ssh config from available information."""
self.__ssh_config[self.hostname] = self.__ssh_config[self.hostname].overridden_by(
_ssh_helpers.SSHConfig(
hostname=self.hostname, port=self.port, user=self.auth.username, identityfile=self.auth.key_filename,
)
)
def __build_connection_chain(self) -> _SSHConnChainT:
conn_chain: _SSHConnChainT = []
config = self.ssh_config[self.hostname]
default_auth = ssh_auth.SSHAuth(username=config.user, key_filename=config.identityfile)
auth = self.__auth_mapping.get_with_alt_hostname(config.hostname, self.hostname, default=default_auth)
conn_chain.append((config, auth))
while config.proxyjump is not None:
# pylint: disable=no-member
config = self.ssh_config[config.proxyjump]
default_auth = ssh_auth.SSHAuth(username=config.user, key_filename=config.identityfile)
conn_chain.append((config, self.__auth_mapping.get(config.hostname, default_auth)))
return conn_chain[::-1]
@property
def auth(self) -> ssh_auth.SSHAuth:
"""Internal authorisation object.
Attention: this public property is mainly for inheritance,
debug and information purposes.
Calls outside SSHClient and child classes is sign of incorrect design.
Change is completely disallowed.
:rtype: ssh_auth.SSHAuth
"""
return self.__auth_mapping[self.hostname]
@property
def hostname(self) -> str:
"""Connected remote host name.
:rtype: str
"""
return self.__hostname
@property
def port(self) -> int:
"""Connected remote port number.
:rtype: int
"""
return self.__port
@property
def ssh_config(self) -> HostsSSHConfigs:
"""SSH connection config.
:rtype: HostsSSHConfigs
"""
return copy.deepcopy(self.__ssh_config)
@property
def is_alive(self) -> bool:
"""Paramiko status: ready to use|reconnect required.
:rtype: bool
"""
return self.__ssh.get_transport() is not None
def __repr__(self) -> str:
"""Representation for debug purposes."""
return f"{self.__class__.__name__}(host={self.hostname}, port={self.port}, auth={self.auth!r})"
def __str__(self) -> str: # pragma: no cover
"""Representation for debug purposes."""
return f"{self.__class__.__name__}(host={self.hostname}, port={self.port}) " f"for user {self.auth.username}"
@property
def _ssh(self) -> paramiko.SSHClient:
"""Ssh client object getter for inheritance support only.
Attention: ssh client object creation and change
is allowed only by __init__ and reconnect call.
:rtype: paramiko.SSHClient
"""
return self.__ssh
@tenacity.retry( # type: ignore
retry=RetryOnExceptions(retry_on=paramiko.SSHException, reraise=paramiko.AuthenticationException),
stop=tenacity.stop_after_attempt(3),
wait=tenacity.wait_fixed(3),
reraise=True,
)
def __connect(self) -> None:
"""Main method for connection open."""
with self.lock:
if self.__sock is not None:
sock = self.__sock
self.__ssh = paramiko.SSHClient()
self.__ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
self.auth.connect(
client=self.__ssh, hostname=self.hostname, port=self.port, log=self.__verbose, sock=sock,
)
else:
self.__ssh = self.__get_client()
transport: paramiko.Transport = self.__ssh.get_transport()
transport.set_keepalive(1 if self.__keepalive_period else 0) # send keepalive packets
def __get_client(self) -> paramiko.SSHClient:
"""Connect using connection chain information.
:return: paramiko ssh connection object
:rtype: paramiko.SSHClient
:raises ValueError: ProxyCommand found in connection chain after first host reached
:raises RuntimeError: Unexpected state
"""
last_ssh_client: paramiko.SSHClient = paramiko.SSHClient()
last_ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
config, auth = self.__conn_chain[0]
if config.proxycommand:
auth.connect(
last_ssh_client,
hostname=config.hostname,
port=config.port or 22,
sock=paramiko.ProxyCommand(config.proxycommand),
)
else:
auth.connect(last_ssh_client, hostname=config.hostname, port=config.port or 22)
for config, auth in self.__conn_chain[1:]: # start has another logic, so do it out of cycle
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
if config.proxyjump:
sock = last_ssh_client.get_transport().open_channel(
kind="direct-tcpip", dest_addr=(config.hostname, config.port or 22), src_addr=(config.proxyjump, 0),
)
auth.connect(ssh, hostname=config.hostname, port=config.port or 22, sock=sock)
last_ssh_client = ssh
continue
if config.proxycommand:
raise ValueError(f"ProxyCommand found in connection chain after first host reached!\n{config}")
raise RuntimeError(f"Unexpected state: Final host by configuration, but requested host is not reached")
return last_ssh_client
def __connect_sftp(self) -> None:
"""SFTP connection opener."""
with self.lock:
try:
self.__sftp = self.__ssh.open_sftp()
except paramiko.SSHException:
self.logger.warning("SFTP enable failed! SSH only is accessible.")
@property
def _sftp(self) -> paramiko.sftp_client.SFTPClient:
"""SFTP channel access for inheritance.
:rtype: paramiko.sftp_client.SFTPClient
:raises paramiko.SSHException: SFTP connection failed
"""
if self.__sftp is not None:
return self.__sftp
self.logger.debug("SFTP is not connected, try to connect...")
self.__connect_sftp()
if self.__sftp is not None:
return self.__sftp
raise paramiko.SSHException("SFTP connection failed")
def close(self) -> None:
"""Close SSH and SFTP sessions."""
with self.lock:
# noinspection PyBroadException
try:
self.__ssh.close()
self.__sftp = None
except Exception:
self.logger.exception("Could not close ssh connection")
if self.__sftp is not None:
# noinspection PyBroadException
try:
self.__sftp.close()
except Exception:
self.logger.exception("Could not close sftp connection")
def __del__(self) -> None:
"""Destructor helper: close channel and threads BEFORE closing others.
Due to threading in paramiko, default destructor could generate asserts on close,
so we calling channel close before closing main ssh object.
"""
try:
self.__ssh.close()
except BaseException as e: # pragma: no cover
self.logger.debug(f"Exception in {self!s} destructor call: {e}")
self.__sftp = None
def __enter__(self) -> "SSHClientBase": # pylint: disable=useless-super-delegation
"""Get context manager."""
# noinspection PyTypeChecker
return super().__enter__()
def __exit__(self, exc_type: typing.Any, exc_val: typing.Any, exc_tb: typing.Any) -> None:
"""Exit context manager.
.. versionchanged:: 1.0.0 disconnect enforced on close
.. versionchanged:: 1.1.0 release lock on exit
.. versionchanged:: 1.2.1 disconnect enforced on close only not in keepalive mode
"""
if not self.__keepalive_period:
self.close()
super().__exit__(exc_type, exc_val, exc_tb)
@property
def sudo_mode(self) -> bool:
"""Persistent sudo mode for connection object.
:rtype: bool
"""
return self.__sudo_mode
@sudo_mode.setter
def sudo_mode(self, mode: bool) -> None:
"""Persistent sudo mode change for connection object.
:param mode: sudo status: enabled | disabled
:type mode: bool
"""
self.__sudo_mode = bool(mode)
@property
def keepalive_period(self) -> int:
"""Keepalive period for connection object.
:rtype: int
If 0 - close connection on exit from context manager.
"""
return self.__keepalive_period
@keepalive_period.setter
def keepalive_period(self, period: typing.Union[int, bool]) -> None:
"""Keepalive period change for connection object.
:param period: keepalive period change
:type period: typing.Union[int, bool]
If 0 - close connection on exit from context manager.
"""
self.__keepalive_period = int(period)
transport: paramiko.Transport = self.__ssh.get_transport()
transport.set_keepalive(int(period))
@property
def keepalive_mode(self) -> int:
"""Keepalive period for connection object.
:rtype: int
If 0 - close connection on exit from context manager.
"""
warnings.warn("keepalive_mode was moved to keepalive_period as time based parameter", DeprecationWarning)
return self.keepalive_period
# noinspection PyDeprecation
@keepalive_mode.setter
def keepalive_mode(self, period: typing.Union[int, bool]) -> None:
"""Keepalive period change for connection object.
:param period: keepalive period change
:type period: typing.Union[int, bool]
If 0 - close connection on exit from context manager.
"""
warnings.warn("keepalive_mode was moved to keepalive_period as time based parameter", DeprecationWarning)
self.keepalive_period = int(period)
def reconnect(self) -> None:
"""Reconnect SSH session."""
with self.lock:
self.close()
self.__connect()
def sudo(self, enforce: typing.Optional[bool] = None) -> "typing.ContextManager[None]":
"""Call contextmanager for sudo mode change.
:param enforce: Enforce sudo enabled or disabled. By default: None
:type enforce: typing.Optional[bool]
:return: context manager with selected sudo state inside
:rtype: typing.ContextManager[None]
"""
return _SudoContext(ssh=self, enforce=enforce)
def keepalive(self, enforce: typing.Union[int, bool] = 1) -> "typing.ContextManager[None]":
"""Call contextmanager with keepalive period change.
:param enforce: Enforce keepalive period.
:type enforce: typing.Union[int, bool]
:return: context manager with selected keepalive state inside
:rtype: typing.ContextManager[None]
.. Note:: Enter and exit ssh context manager is produced as well.
.. versionadded:: 1.2.1
"""
return _KeepAliveContext(ssh=self, enforce=int(enforce))
def _prepare_command(self, cmd: str, chroot_path: typing.Optional[str] = None) -> str:
"""Prepare command: cower chroot and other cases.
:param cmd: main command
:param chroot_path: path to make chroot for execution
:return: final command, includes chroot, if required
"""
if not self.sudo_mode:
return super()._prepare_command(cmd=cmd, chroot_path=chroot_path)
if any((chroot_path, self._chroot_path)):
target_path: str = shlex.quote(chroot_path if chroot_path else self._chroot_path) # type: ignore
quoted_command: str = shlex.quote(cmd)
return f'chroot {target_path} sudo sh -c {shlex.quote(f"eval {quoted_command}")}'
return f'sudo -S sh -c "eval {shlex.quote(cmd)}"'
# noinspection PyMethodOverriding
def _execute_async( # pylint: disable=arguments-differ
self,
command: str,
*,
stdin: OptionalStdinT = None,
open_stdout: bool = True,
open_stderr: bool = True,
chroot_path: typing.Optional[str] = None,
get_pty: bool = False,
width: int = 80,
height: int = 24,
**kwargs: typing.Any,
) -> SshExecuteAsyncResult:
"""Execute command in async mode and return channel with IO objects.
:param command: Command for execution
:type command: str
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param chroot_path: chroot path override
:type chroot_path: typing.Optional[str]
:param get_pty: Get PTY for connection
:type get_pty: bool
:param width: PTY width
:type width: int
:param height: PTY height
:type height: int
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Tuple with control interface and file-like objects for STDIN/STDERR/STDOUT
:rtype: typing.NamedTuple(
'SshExecuteAsyncResult',
[
('interface', paramiko.Channel),
('stdin', paramiko.ChannelFile),
('stderr', typing.Optional[paramiko.ChannelFile]),
('stdout', typing.Optional[paramiko.ChannelFile]),
("started", datetime.datetime),
]
)
.. versionchanged:: 1.2.0 open_stdout and open_stderr flags
.. versionchanged:: 1.2.0 stdin data
.. versionchanged:: 1.2.0 get_pty moved to `**kwargs`
.. versionchanged:: 2.1.0 Use typed NamedTuple as result
.. versionchanged:: 3.2.0 Expose pty options as optional keyword-only arguments
.. versionchanged:: 4.1.0 support chroot
"""
chan: paramiko.Channel = self._ssh.get_transport().open_session()
if get_pty:
# Open PTY
chan.get_pty(term="vt100", width=width, height=height, width_pixels=0, height_pixels=0)
_stdin: paramiko.ChannelFile = chan.makefile("wb")
stdout: paramiko.ChannelFile = chan.makefile("rb")
stderr: typing.Optional[paramiko.ChannelFile] = chan.makefile_stderr("rb") if open_stderr else None
cmd = f"{self._prepare_command(cmd=command, chroot_path=chroot_path)}\n"
started = datetime.datetime.utcnow()
if self.sudo_mode:
chan.exec_command(cmd) # nosec # Sanitize on caller side
if stdout.channel.closed is False:
# noinspection PyTypeChecker
self.auth.enter_password(_stdin)
_stdin.flush()
else:
chan.exec_command(cmd) # nosec # Sanitize on caller side
if stdin is not None:
if not _stdin.channel.closed:
stdin_str: bytes = self._string_bytes_bytearray_as_bytes(stdin)
_stdin.write(stdin_str)
_stdin.flush()
else:
self.logger.warning("STDIN Send failed: closed channel")
if open_stdout:
res_stdout = stdout
else:
stdout.close()
res_stdout = None
return SshExecuteAsyncResult(interface=chan, stdin=_stdin, stderr=stderr, stdout=res_stdout, started=started)
def _exec_command( # type: ignore
self,
command: str,
async_result: SshExecuteAsyncResult,
timeout: OptionalTimeoutT,
*,
verbose: bool = False,
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Get exit status from channel with timeout.
:param command: executed command (for logs)
:type command: str
:param async_result: execute_async result
:type async_result: SshExecuteAsyncResult
:param timeout: timeout before stop execution with TimeoutError
:type timeout: typing.Union[int, float, None]
:param verbose: produce log.info records for STDOUT/STDERR
:type verbose: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises ExecHelperTimeoutError: Timeout exceeded
.. versionchanged:: 1.2.0 log_mask_re regex rule for masking cmd
"""
def poll_streams() -> None:
"""Poll FIFO buffers if data available."""
if async_result.stdout and async_result.interface.recv_ready():
result.read_stdout(src=async_result.stdout, log=self.logger, verbose=verbose)
if async_result.stderr and async_result.interface.recv_stderr_ready():
result.read_stderr(src=async_result.stderr, log=self.logger, verbose=verbose)
@threaded.threadpooled
def poll_pipes() -> None:
"""Polling task for FIFO buffers."""
while not async_result.interface.status_event.is_set():
time.sleep(0.1)
if async_result.stdout or async_result.stderr:
poll_streams()
result.read_stdout(src=async_result.stdout, log=self.logger, verbose=verbose)
result.read_stderr(src=async_result.stderr, log=self.logger, verbose=verbose)
result.exit_code = async_result.interface.exit_status
# channel.status_event.wait(timeout)
cmd_for_log: str = self._mask_command(cmd=command, log_mask_re=log_mask_re)
# Store command with hidden data
result = exec_result.ExecResult(cmd=cmd_for_log, stdin=stdin, started=async_result.started)
# noinspection PyNoneFunctionAssignment,PyTypeChecker
future: "concurrent.futures.Future[None]" = poll_pipes()
concurrent.futures.wait([future], timeout)
# Process closed?
if async_result.interface.status_event.is_set():
async_result.interface.close()
return result
async_result.interface.close()
async_result.interface.status_event.set()
future.cancel()
concurrent.futures.wait([future], 0.001)
result.set_timestamp()
wait_err_msg: str = _log_templates.CMD_WAIT_ERROR.format(result=result, timeout=timeout)
self.logger.debug(wait_err_msg)
raise exceptions.ExecHelperTimeoutError(result=result, timeout=timeout) # type: ignore
def execute( # pylint: disable=arguments-differ
self,
command: str,
verbose: bool = False,
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
*,
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
open_stdout: bool = True,
open_stderr: bool = True,
get_pty: bool = False,
width: int = 80,
height: int = 24,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Execute command and wait for return code.
:param command: Command for execution
:type command: str
:param verbose: Produce log.info records for command call and output
:type verbose: bool
:param timeout: Timeout for command execution.
:type timeout: typing.Union[int, float, None]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param get_pty: Get PTY for connection
:type get_pty: bool
:param width: PTY width
:type width: int
:param height: PTY height
:type height: int
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises ExecHelperTimeoutError: Timeout exceeded
.. versionchanged:: 1.2.0 default timeout 1 hour
.. versionchanged:: 2.1.0 Allow parallel calls
"""
return super().execute(
command=command,
verbose=verbose,
timeout=timeout,
log_mask_re=log_mask_re,
stdin=stdin,
open_stdout=open_stdout,
open_stderr=open_stderr,
get_pty=get_pty,
width=width,
height=height,
**kwargs,
)
def __call__(
self,
command: str,
verbose: bool = False,
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
*,
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
open_stdout: bool = True,
open_stderr: bool = True,
get_pty: bool = False,
width: int = 80,
height: int = 24,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Execute command and wait for return code.
:param command: Command for execution
:type command: str
:param verbose: Produce log.info records for command call and output
:type verbose: bool
:param timeout: Timeout for command execution.
:type timeout: typing.Union[int, float, None]
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param get_pty: Get PTY for connection
:type get_pty: bool
:param width: PTY width
:type width: int
:param height: PTY height
:type height: int
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises ExecHelperTimeoutError: Timeout exceeded
.. versionchanged:: 1.2.0 default timeout 1 hour
.. versionchanged:: 2.1.0 Allow parallel calls
"""
return super().__call__(
command=command,
verbose=verbose,
timeout=timeout,
log_mask_re=log_mask_re,
stdin=stdin,
open_stdout=open_stdout,
open_stderr=open_stderr,
get_pty=get_pty,
width=width,
height=height,
**kwargs,
)
def check_call( # pylint: disable=arguments-differ
self,
command: str,
verbose: bool = False,
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
error_info: typing.Optional[str] = None,
expected: typing.Iterable[ExitCodeT] = (proc_enums.EXPECTED,),
raise_on_err: bool = True,
*,
log_mask_re: typing.Optional[str] = None,
stdin: OptionalStdinT = None,
open_stdout: bool = True,
open_stderr: bool = True,
get_pty: bool = False,
width: int = 80,
height: int = 24,
exception_class: CalledProcessErrorSubClassT = exceptions.CalledProcessError,
**kwargs: typing.Any,
) -> exec_result.ExecResult:
"""Execute command and check for return code.
:param command: Command for execution
:type command: str
:param verbose: Produce log.info records for command call and output
:type verbose: bool
:param timeout: Timeout for command execution.
:type timeout: typing.Union[int, float, None]
:param error_info: Text for error details, if fail happens
:type error_info: typing.Optional[str]
:param expected: expected return codes (0 by default)
:type expected: typing.Iterable[typing.Union[int, proc_enums.ExitCodes]]
:param raise_on_err: Raise exception on unexpected return code
:type raise_on_err: bool
:param log_mask_re: regex lookup rule to mask command for logger.
all MATCHED groups will be replaced by '<*masked*>'
:type log_mask_re: typing.Optional[str]
:param stdin: pass STDIN text to the process
:type stdin: typing.Union[bytes, str, bytearray, None]
:param open_stdout: open STDOUT stream for read
:type open_stdout: bool
:param open_stderr: open STDERR stream for read
:type open_stderr: bool
:param get_pty: Get PTY for connection
:type get_pty: bool
:param width: PTY width
:type width: int
:param height: PTY height
:type height: int
:param exception_class: Exception class for errors. Subclass of CalledProcessError is mandatory.
:type exception_class: typing.Type[exceptions.CalledProcessError]
:param kwargs: additional parameters for call.
:type kwargs: typing.Any
:return: Execution result
:rtype: ExecResult
:raises ExecHelperTimeoutError: Timeout exceeded
:raises CalledProcessError: Unexpected exit code
.. versionchanged:: 1.2.0 default timeout 1 hour
.. versionchanged:: 3.2.0 Exception class can be substituted
.. versionchanged:: 3.4.0 Expected is not optional, defaults os dependent
"""
return super().check_call(
command=command,
verbose=verbose,
timeout=timeout,
error_info=error_info,