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

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.8.3.zip (511.2 kB view details)

Uploaded Source

Built Distributions

exec_helpers-0.8.3-cp36-none-win_amd64.whl (366.0 kB view details)

Uploaded CPython 3.6 Windows x86-64

exec_helpers-0.8.3-cp36-none-win32.whl (317.5 kB view details)

Uploaded CPython 3.6 Windows x86

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

Uploaded CPython 3.6m

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

Uploaded CPython 3.6m

exec_helpers-0.8.3-cp35-none-win_amd64.whl (357.9 kB view details)

Uploaded CPython 3.5 Windows x86-64

exec_helpers-0.8.3-cp35-none-win32.whl (311.4 kB view details)

Uploaded CPython 3.5 Windows x86

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

Uploaded CPython 3.5m

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

Uploaded CPython 3.5m

exec_helpers-0.8.3-cp34-none-win_amd64.whl (349.7 kB view details)

Uploaded CPython 3.4 Windows x86-64

exec_helpers-0.8.3-cp34-none-win32.whl (317.1 kB view details)

Uploaded CPython 3.4 Windows x86

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

Uploaded CPython 3.4m

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

Uploaded CPython 3.4m

exec_helpers-0.8.3-cp27-none-win_amd64.whl (360.1 kB view details)

Uploaded CPython 2.7 Windows x86-64

exec_helpers-0.8.3-cp27-none-win32.whl (317.4 kB view details)

Uploaded CPython 2.7 Windows x86

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

Uploaded CPython 2.7mu

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

Uploaded CPython 2.7mu

File details

Details for the file exec-helpers-0.8.3.zip.

File metadata

  • Download URL: exec-helpers-0.8.3.zip
  • Upload date:
  • Size: 511.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for exec-helpers-0.8.3.zip
Algorithm Hash digest
SHA256 c62fc50af9581ab6ef2a770375dbad4a9860759e9bf5a51b3fd6dd8edf518d76
MD5 bcf9be374e5a5333c59d63b308bb1468
BLAKE2b-256 7c788018d8ecbc5c3d9c17fde69cc8e422f911b79b6a7246680d50ae83e88554

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp36-none-win_amd64.whl
Algorithm Hash digest
SHA256 a549e880d746fff06f6a11165663e64a86b736813aad3d374976cbe560fc16c0
MD5 07fab918097cebcccb23e95f1702a53e
BLAKE2b-256 19b6697e3a34a56dbf2b885f0b49cb70d7870a2fb9a05e7f54a3c082b272dbdb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp36-none-win32.whl
Algorithm Hash digest
SHA256 5a94a7fb79e55b3cba778a22031800d75d620109592859020f37e805dd13f64a
MD5 327b414db5e434783e76b5f2a8d97137
BLAKE2b-256 c4bc221afe4dd94f72a9565ed9b78092e1d483b784b4aa78dc1555dee7ebb186

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp36-cp36m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ee6e4b388419c9a5db9f157ba82daca188179a10e7af90a5f93279f27c2270a5
MD5 a4c967be77da03ad852040131c0fdf77
BLAKE2b-256 a98d22ca93a63c90a2d2855ba946e833d29b2107881f00111dc0cd47baf9b939

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp36-cp36m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 c64dccbc901463c8ebc51c3b320dd7ce105d4ee59bdaafda5be35a8690cf03c0
MD5 222a964eeb8692a6e5638804c163a2b4
BLAKE2b-256 67256dc947545580a21b7015b12e10c88252a1b294ce4c3b3d49b1accb50ae65

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp35-none-win_amd64.whl
Algorithm Hash digest
SHA256 8f07f7b4e45cb8f7937496185ecb71610889b99848f690df6317aa019de36aab
MD5 2b0ae4968c4017401e52022f1e47bd15
BLAKE2b-256 dcbc466c3057c896cfe554cba4bf84fd4fc13cc516f70c093a06cb4572b65a24

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp35-none-win32.whl
Algorithm Hash digest
SHA256 cc0e957adc4bfa83f558ce28cc59b5083b49a5fa4c969bca5f68973737176cc4
MD5 90feb2ea0fce0508d62eabfe1e5e0c2c
BLAKE2b-256 eda0d39bedbac6b15f0dd99d593a39b9eef3fe76e2cbfcf19bcd4bfe1b53afcf

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp35-cp35m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 3e79f66b7de993ea63a4d4e42ce5e23362f4af10738debfb278d3af3ea73cce3
MD5 4495196ea578f393a32f1d822d3f924a
BLAKE2b-256 1295033912af08e20b2dbc29fd9264fe64925cd418f0024a951b3f590ea9594c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp35-cp35m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 b94ca55334c83abad83d38bbce71bcfe4e4f9944156ac5d2f052d6f239422909
MD5 6d10bc97316b1fe7b58d7e98652b60b2
BLAKE2b-256 4892f63e75bd72ed7fb446f8531f7fe53c52203496492a7bc3d7a3955cea1073

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp34-none-win_amd64.whl
Algorithm Hash digest
SHA256 e872634d3ca378b8cef133d5ed4e908b194c1b698317517f548b1ead08338134
MD5 ef79a74575504d3dcd465e8c584533e0
BLAKE2b-256 e757c1e4d227e821e217e5f767194c78c84fe84d4a2bbe41627046922a48aa7c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp34-none-win32.whl
Algorithm Hash digest
SHA256 25532bfb45bbb6234f8eb5158e92843e62012e783e9ca245be553b7d33d348ef
MD5 468e2bfb1acb13c3bd18d2908f658405
BLAKE2b-256 33952fabb22d3c02edebbcbf013195e83a10069e13b083c584c0dad31451d1ea

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp34-cp34m-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 ab37ccc6cb13a4f9880dcaea29acef57a81b3ed7cec532571d8aa4c0026ab437
MD5 42c9956293288a6151d4d94c9a7376e0
BLAKE2b-256 680c91c794882502d538c6e09363dbb86edc606d7bfb5f92f8ae497be55cb209

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp34-cp34m-manylinux1_i686.whl
Algorithm Hash digest
SHA256 71231de60f39124bf2ed2c20611571685a8d8a6c45147d6e8af8d1e321fd799d
MD5 f4c6d92a1ee4fe6e6ee61207d85cd409
BLAKE2b-256 fcd296e2a97049c3d3b73aa74ced07d7aedfc9f2d91d46957a69337c65d61829

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp27-none-win_amd64.whl
Algorithm Hash digest
SHA256 33590c83da91303ecbf581e2a06fff68cc9d991b60c1e3ad70f026216b3df98b
MD5 4c94fda4eb6b7b887f95349a6f3d4fa3
BLAKE2b-256 cf58c59d083b2ae732ba74846479f55df0411e0ede5ad482bf54afec1e7a518e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp27-none-win32.whl
Algorithm Hash digest
SHA256 aea8ac0b67106ae238b7a80ece00c0d3d890a269b6c0e7740a3086e030a0e650
MD5 21abfeaeeb95207039627ef4caa7617e
BLAKE2b-256 57f283fb66abdd70f7b881a5218eb47200db8f87f33ef4941cbfbc6ea1f9c445

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm Hash digest
SHA256 5075b3921e885f166827a1c6a01e5df1c929ec1d3897343e0435a4ab3cc22141
MD5 c7cc4671add7045bf1005768314e0cb1
BLAKE2b-256 b285e9f85e8ff89481d11ee41d6a9f3b8d0846540096fa55c57fe95813131c29

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for exec_helpers-0.8.3-cp27-cp27mu-manylinux1_i686.whl
Algorithm Hash digest
SHA256 5d6347571a0d55f2671e998f854616b6766eb7dfd4ab2d0954be8b8e52410f6f
MD5 dea910ad66d7af2bd5d3b0a3a451d86d
BLAKE2b-256 ced09c220b1e173e295230811f6606dda9a11636f33c6ce399d364157a6f880b

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