Skip to main content

Execution helpers for simplified usage of subprocess and ssh.

Project description

exec-helpers

https://travis-ci.org/penguinolog/exec-helpers.svg?branch=master https://img.shields.io/appveyor/ci/penguinolog/exec-helpers.svg https://coveralls.io/repos/github/penguinolog/exec-helpers/badge.svg?branch=master https://img.shields.io/pypi/v/exec-helpers.svg https://img.shields.io/pypi/pyversions/exec-helpers.svg https://img.shields.io/pypi/status/exec-helpers.svg https://img.shields.io/github/license/penguinolog/exec-helpers.svg

Execution helpers for simplified usage of subprocess and ssh. Why another subprocess wrapper and why no clear paramiko?

Historically paramiko offers good ssh client, but with specific limitations: you can call command with timeout, but without receiving return code, or call command and wait for return code, but without timeout processing.

In the most cases, we are need just simple SSH client with comfortable API for calls, calls via SSH proxy and checking return code/stderr. This library offers this functionality with connection memorizing, deadlock free polling and friendly result objects (with inline decoding of YAML, JSON, binary or just strings). In addition this library offers the same API for subprocess calls, but with specific limitation: no parallel calls (for protection from race conditions).

Pros:

Python 2.7
Python 3.4
Python 3.5
Python 3.6
PyPy
PyPy3 3.5+

This package includes:

  • SSHClient - historically the first one helper, which used for SSH connections and requires memorization due to impossibility of connection close prediction. Several API calls for sFTP also presents.

  • SSHAuth - class for credentials storage. SSHClient does not store credentials as-is, but uses SSHAuth for it. Objects of this class can be copied between ssh connection objects, also it used for execute_through_host.

  • Subprocess - subprocess.Popen wrapper with timeouts, polling and almost the same API, as SSHClient (except specific flags, like cwd for subprocess and get_tty for ssh).

  • ExecResult - class for execution results storage. Contains exit code, stdout, stderr and getters for decoding as JSON, YAML, string, bytearray and brief strings (up to 7 lines).

  • ExitCodes - enumerator for standard Linux exit codes. BASH return codes (broduced from signal codes) also available.

Usage

SSHClient

Basic initialization of SSHClient can be done without construction of specific objects:

client = exec_helpers.SSHClient(host, username="username", password="password")
If ssh agent is running - keys will be collected by paramiko automatically, but if keys are in specific location
  • it should be loaded manually and providen as iterable object of paramiko.RSAKey.

For advanced cases or re-use of credentials, SSHAuth object should be used. It can be collected from connection object via property auth.

Creation from scratch:

auth = exec_helpers.SSHAuth(
    username='username',  # type: typing.Optional[str]
    password='password',  # type: typing.Optional[str]
    key=None,  # type: typing.Optional[paramiko.RSAKey]
    keys=None,
)

Key is a main connection key (always tried first) and keys are alternate keys. If main key now correct for username - alternate keys tried, if correct key found - it became main. If no working key - password is used and None is set as main key.

Subprocess

No initialization required.

Base methods

Main methods are execute, check_call and check_stderr for simple executing, executing and checking return code and executing, checking return code and checking for empty stderr output. This methods are almost the same for SSHCleint and Subprocess, except specific flags.

result = helper.execute(
    command,  # type: str
    verbose=False,  # type: bool
    timeout=None,  # type: typing.Optional[int]
    **kwargs
)
result = helper.check_call(
    command,  # type: str
    verbose=False,  # type: bool
    timeout=None,  # type: typing.Optional[int]
    error_info=None,  # type: typing.Optional[str]
    expected=None,  # type: typing.Optional[typing.Iterable[int]]
    raise_on_err=True,  # type: bool
    **kwargs
)
result = helper.check_stderr(
    command,  # type: str
    verbose=False,  # type: bool
    timeout=None,  # type: typing.Optional[int]
    error_info=None,  # type: typing.Optional[str]
    raise_on_err=True,  # type: bool
)

The next command level uses lower level and kwargs are forwarded, so expected exit codes are forwarded from check_stderr. Implementation specific flags are always set via kwargs.

ExecResult

Execution result object has a set of useful properties:

  • cmd - Command

  • exit_code - Command return code. If possible to decode using enumerators for Linux -> it used.

  • stdout -> typing.Tuple[bytes]. Raw stdout output.

  • stderr -> typing.Tuple[bytes]. Raw stderr output.

  • stdout_bin -> bytearray. Binary stdout output.

  • stderr_bin -> bytearray. Binary stderr output.

  • stdout_str -> six.text_types. Text representation of output.

  • stderr_str -> six.text_types. Text representation of output.

  • stdout_brief -> six.text_types. Up to 7 lines from stdout (3 first and 3 last if >7 lines).

  • stderr_brief -> six.text_types. Up to 7 lines from stderr (3 first and 3 last if >7 lines).

  • stdout_json - STDOUT decoded as JSON.

  • stdout_yaml - STDOUT decoded as YAML

  • timestamp -> typing.Optional(datetime.datetime). Timestamp for received exit code.

SSHClient specific

SSHClient commands support get_pty flag, which enables PTY open on remote side. PTY width and height can be set via kwargs, dimensions in pixels are always 0x0.

Possible to call commands in parallel on multiple hosts if it’s not produce huge output:

results = SSHClient.execute_together(remotes, command, timeout=None, expected=None, raise_on_err=True)
results  # type: ttyping.Dict[typing.Tuple[str, int], exec_result.ExecResult]

Results is a dict with keys = (hostname, port) and and results in values. By default execute_together raises exception if unexpected return code on any remote.

For execute through SSH host can be used execute_through_host method:

result = client.execute_through_host(
    hostname,  # type: str
    command,  # type: str
    auth=None,  # type: typing.Optional[SSHAuth]
    target_port=22,  # type: int
    timeout=None,  # type: typing.Optional[int]
    verbose=False,  # type: bool
    get_pty=False,  # type: bool
)

Where hostname is a target hostname, auth is an alternate credentials for target host.

SSH client implements fast sudo support via context manager: Commands will be run with sudo enforced independently from client settings for normal usage:

with client.sudo(enforce=True):
    ...

Commands will be run without sudo independently from client settings for normal usage:

with client.sudo(enforce=False):
    ...

“Permanent client setting”:

client.sudo_mode = mode  # where mode is True or False

SSH Client supports sFTP for working with remote files:

with client.open(path, mode='r'):
    ...

For fast remote paths checks available methods: exists, stat, isfile and isdir. All of them receives remote path and returns single result (stat -> paramiko.sftp_attr.SFTPAttributes, others bool).

Testing

The main test mechanism for the package exec-helpers is using tox. Test environments available:

pep8
py27
py34
py35
py36
pylint
pep257

CI systems

For code checking several CI systems is used in parallel:

  1. Travis CI: is used for checking: PEP8, pylint, bandit, installation possibility and unit tests. Also it’s publishes coverage on coveralls.

  2. AppVeyor: is used for checking windows compatibility.

  3. coveralls: is used for coverage display.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exec-helpers-0.9.0.tar.gz (482.1 kB view details)

Uploaded Source

Built Distributions

exec_helpers-0.9.0-cp36-none-win_amd64.whl (366.4 kB view details)

Uploaded CPython 3.6 Windows x86-64

exec_helpers-0.9.0-cp36-none-win32.whl (318.2 kB view details)

Uploaded CPython 3.6 Windows x86

exec_helpers-0.9.0-cp36-cp36m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.6m

exec_helpers-0.9.0-cp36-cp36m-manylinux1_i686.whl (1.5 MB view details)

Uploaded CPython 3.6m

exec_helpers-0.9.0-cp35-none-win_amd64.whl (358.6 kB view details)

Uploaded CPython 3.5 Windows x86-64

exec_helpers-0.9.0-cp35-none-win32.whl (312.5 kB view details)

Uploaded CPython 3.5 Windows x86

exec_helpers-0.9.0-cp35-cp35m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.5m

exec_helpers-0.9.0-cp35-cp35m-manylinux1_i686.whl (1.5 MB view details)

Uploaded CPython 3.5m

exec_helpers-0.9.0-cp34-none-win_amd64.whl (350.9 kB view details)

Uploaded CPython 3.4 Windows x86-64

exec_helpers-0.9.0-cp34-none-win32.whl (317.9 kB view details)

Uploaded CPython 3.4 Windows x86

exec_helpers-0.9.0-cp34-cp34m-manylinux1_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.4m

exec_helpers-0.9.0-cp34-cp34m-manylinux1_i686.whl (1.5 MB view details)

Uploaded CPython 3.4m

exec_helpers-0.9.0-cp27-none-win_amd64.whl (361.3 kB view details)

Uploaded CPython 2.7 Windows x86-64

exec_helpers-0.9.0-cp27-none-win32.whl (318.0 kB view details)

Uploaded CPython 2.7 Windows x86

exec_helpers-0.9.0-cp27-cp27mu-manylinux1_x86_64.whl (1.5 MB view details)

Uploaded CPython 2.7mu

exec_helpers-0.9.0-cp27-cp27mu-manylinux1_i686.whl (1.3 MB view details)

Uploaded CPython 2.7mu

File details

Details for the file exec-helpers-0.9.0.tar.gz.

File metadata

File hashes

Hashes for exec-helpers-0.9.0.tar.gz
Algorithm Hash digest
SHA256 5edd6d13ac53cbf6477497b7710268570c16bb1e6ccafb27c46598b5664ab997
MD5 d714dab344237abd9d86b6fd0503716d
BLAKE2b-256 7ec2f68a49058ee32bd3ac033755e7cc1e206c205f98b606026ecb4b42d6dcb9

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp36-none-win_amd64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 06d47da25c0eb0a662905ced6da816f3b8c6194aca9fcf33971994a1005ff263
MD5 13edaa7861470fb79611bc4c7648b131
BLAKE2b-256 f94c10ad38b7a605fafdf0d884883c5270ef73596231dc0693ba4d783489e751

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp36-none-win32.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp36-none-win32.whl
Algorithm Hash digest
SHA256 19491f7f427da0f2ea330475d110c0b47dcab4085a3f01a74928cba5e7afea06
MD5 38b472aaba8bc3531060ea0beb063836
BLAKE2b-256 835a360fb778f0ec36b45daa5f37c18c0e45c3d98ab9f1a44ce9cd8524dec129

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp36-cp36m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3163a2be196cba9c98c986aec0324bea21d435d43581ebf78e650f05f47db262
MD5 ea21f9a9e155e767815abf444dc627cf
BLAKE2b-256 56f34d762fbd48b2ad87b9eaaf84ac82d540c90445cd56d5e088a549b79539a1

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp36-cp36m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 78e90efe069e379aac312b7783465ac40ac8ae6f77838e18870ca60e66e78582
MD5 41a8e17b1a7d3e81383d1b7bfa2c5787
BLAKE2b-256 0006ae540b06f156b0d95cb3468e532d8bbeaff32e86caf5c36574f1bb109803

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp35-none-win_amd64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp35-none-win_amd64.whl
Algorithm Hash digest
SHA256 59d40a5dc7fe335ddfe05a97eb53c28894805b6865ad79786da356aa424cd629
MD5 e7420e27ef22dd0606f168897aac5aa6
BLAKE2b-256 5b69b5fb707f4b340bb3214afea58b0b95c639bdfe6c70d281bb522c9429b293

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp35-none-win32.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp35-none-win32.whl
Algorithm Hash digest
SHA256 9d616bf8df83318efd7d2eb705b05d2d046080b34a93e21aee2aaa7fe58e69f2
MD5 2fdb593bf37a31769c2ecc34ac39eb21
BLAKE2b-256 0680dce64f7c152ff78e1d41bef40c58fcb4facf78b1a33875f1633878eb48dd

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp35-cp35m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 9977afee2a6a6db705ed5650586c5e98b860758e381215d44807d52ff925fb06
MD5 579c2769b14981fc339d6b4f29531d47
BLAKE2b-256 a76f1365f0579b357467a93522cc230550c61163181b01d06c38ca44bafa3ad4

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp35-cp35m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 7decbdf8aff93abc81e332311b138f65808da6d8462765c284e0d759e935eebe
MD5 4e15d4a16d82fe5576a5ff9772d7dbf4
BLAKE2b-256 debe1c402e931ffc464f4ec0a512a37b064c039229f9ee085e54f0d8137f933e

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp34-none-win_amd64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp34-none-win_amd64.whl
Algorithm Hash digest
SHA256 12130e2670f2a2bbd568b318abbff0a22e5938fc51960c539db6592500fdd573
MD5 6c5d460165416e8ccfb7d271bc9d0565
BLAKE2b-256 c77bb50f9b755af82db6beda3aeb845ffbf7cafe864b15c5ffbebc64740c850b

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp34-none-win32.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp34-none-win32.whl
Algorithm Hash digest
SHA256 2874bf2ea2a8b3658b0b6ff5fa3044fc20c498a2d626f15a3976804238e8c770
MD5 3ad33bd9f0b14ba01c0cdff25078401b
BLAKE2b-256 189bcb9db7bc991757943f18df2e8df2e23b2b2b7c38581344067b8fb6b2d2c8

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp34-cp34m-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 a025636362ea573ae06d9a05305e74b9e05d932ab1272b1962cba2c1a7fb6119
MD5 85e3e8c23838f4f8f371831d0230f2ad
BLAKE2b-256 7537d697a4141bf673dd38edb182abf405975da16fcfe1827dbd2f488fbd7b04

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp34-cp34m-manylinux1_i686.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 85fe58fe69ad49afb17cead8d1548ba394e0badc928ac91f0792b1a1b8c9ab80
MD5 7757732d8cfb023681210cbc96f3b5f4
BLAKE2b-256 c8091b8df17d5d0181c884cd25f1995220c2c91bf504220cd3573a058c64691e

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp27-none-win_amd64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp27-none-win_amd64.whl
Algorithm Hash digest
SHA256 e646ab996797765437ca3b6ebd4db9840cf25e08bfcafa04a6630b5ed4d8b362
MD5 df12cc7d7fbd3bf86667f12dfe42e732
BLAKE2b-256 92a34b1df467546e362fb2b12af6b4051f9ae418062836e99108962f94604e50

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp27-none-win32.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp27-none-win32.whl
Algorithm Hash digest
SHA256 858750e95309a92c55fe7fa1d48b6c45feb9ad24793929eadbd1d3eb7a75e589
MD5 154edc77f0c67627cdd3848a1098531f
BLAKE2b-256 a3c3039d90cd5704e82f355900c17f193118dec5dd9409b011fe6e2d2be2a666

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp27-cp27mu-manylinux1_x86_64.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 b8fb373e09b616a0b1dc70fbeba5f5750eb95dbf29b68c69e07c7f0e61de759b
MD5 5f70d6a1a7bd78560313eaa925e53872
BLAKE2b-256 09371956e2b32c808e1f6d61cff480a979d755004d0cb1c5cd274a5121890856

See more details on using hashes here.

File details

Details for the file exec_helpers-0.9.0-cp27-cp27mu-manylinux1_i686.whl.

File metadata

File hashes

Hashes for exec_helpers-0.9.0-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 d04de9f117d2a30d7362b21ccd2f87934ed43a8ba604b97830edbf9c428536f9
MD5 985eff66b05dbe449119e3bd45e3a4c2
BLAKE2b-256 07c5c85ef74055a47123e9e1c6c0fe8b3707322d21e3f6b2bfadd48a3d3dc386

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page