Skip to main content

Python library for remote ssh calls through a gateway.

Project description

https://secure.travis-ci.org/AmadeusITGroup/JumpSSH.svg?branch=master https://coveralls.io/repos/AmadeusITGroup/JumpSSH/badge.svg?branch=master https://badge.fury.io/py/jumpssh.svg https://readthedocs.org/projects/jumpssh/badge?version=latest https://sonarcloud.io/api/project_badges/measure?project=amadeusitgroup_jumpssh&metric=bugs https://sonarcloud.io/api/project_badges/measure?project=amadeusitgroup_jumpssh&metric=vulnerabilities
JumpSSH:

Python module to run commands on remote servers

Copyright:

Copyright (c) 2017 Amadeus sas

License:

MIT

Documentation:

https://jumpssh.readthedocs.io

Development:

https://github.com/AmadeusITGroup/JumpSSH

What

JumpSSH is a module for Python 2.7+/3.5+ that can be used to run commands on remote servers through a gateway.

It is based on paramiko library. It provides the ability to execute commands on hosts that are not directly accessible but only through one or more servers. Script does not need to be uploaded on a remote server and can be run locally.

Several authentication methods are supported (password, ssh key).

Commands can be run through several jump servers before reaching the remote server. No need to establish a session for each command, a single ssh session can run as many command as you want, including parallel queries, and you will get result for each command independently.

So, why another python library to setup remote server through ssh ? Here is a quick comparison with the most known existing python libraries
  • Paramiko: provide very good implementation of SSHv2 protocol in python but with a low level api a bit complex

  • Ansible: require more configuration and understanding to start. Moreover, support of bastion host is done with modification of local ssh config to use ProxyCommand, and this is needed for each bastion host.

  • Fabric: use of jump server is much easier than Ansible thanks to ‘env.gateway’ parameter, but does not allow jump through several servers.

Installation

To install JumpSSH, simply:

$ pip install jumpssh

Examples

establish ssh session with a remote host through a gateway:

>>> from jumpssh import SSHSession

# establish ssh connection between your local machine and the jump server
>>> gateway_session = SSHSession('gateway.example.com',
...                              'my_user', password='my_password').open()

# from jump server, establish connection with a remote server
>>> remote_session = gateway_session.get_remote_session('remote.example.com',
...                                                     password='my_password2')

run commands on remote host:

# command will be executed remotely and output will be returned locally and printed
>>> print(remote_session.get_cmd_output('ls -lta'))
total 28
drwxr-xr-x. 412 root    root    12288 Mar 21 14:25 ..
drwx------.   2 my_user my_user    28 Mar  6 19:25 .ssh
drwx------.   3 my_user my_user    70 Mar  6 19:25 .
-rw-r--r--.   1 my_user my_user    18 Jul 12  2016 .bash_logout
-rw-r--r--.   1 my_user my_user   193 Jul 12  2016 .bash_profile
-rw-r--r--.   1 my_user my_user   231 Jul 12  2016 .bashrc

# get exit code of the remotely executed command (here to check if a package is installed)
>>> remote_session.get_exit_code('yum list installed package_name')
0

remote rest api usage:

# calling rest api on remote host that is only accessible from the gateway
>>> from jumpssh import RestSshClient
>>> rest_client = RestSshClient(gateway_session)

# syntax is similar to requests library (http://docs.python-requests.org)
>>> http_response = rest_client.get('http://remote.example.com/helloworld')
>>> http_response.status_code
200
>>> http_response.text
u'Hello, World!'

remote files operations:

# check if remote path exists
>>> remote_session.exists('/path/to/a/file')
True

# copy file from local machine to remote host through gateway
>>> remote_session.put('/local/path/to/a/file', '/remote/path/to/the/file')

# create file on remote host from local content
>>> remote_session.file('/remote/path/to/the/file',
...                     content='remote file content', permissions='600')

# download remote file on local machine from remote host through gateway
>>> remote_session.get('/remote/path/to/the/file', '/local/path/')

Tests

jumpssh tests require docker, check docker documentation for how to install it depending on your OS. it also requires few python packages. To install them, run:

$ pip install -r requirements_dev.txt

To run the test suite, clone the repository and run:

$ pytest -sv tests/

or simply:

$ tox

Contributing

Bug Reports

Bug reports are hugely important! Before you raise one, though, please check through the GitHub issues, both open and closed, to confirm that the bug hasn’t been reported before.

Feature Requests

If you think a feature is missing and could be useful in this module, feel free to raise a feature request through the GitHub issues

Code Contributions

When contributing code, please follow this project-agnostic contribution guide.

Download files

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

Source Distribution

jumpssh-1.6.5.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

jumpssh-1.6.5-py3-none-any.whl (32.1 kB view details)

Uploaded Python 3

File details

Details for the file jumpssh-1.6.5.tar.gz.

File metadata

  • Download URL: jumpssh-1.6.5.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.20.1 setuptools/38.5.2 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.7.1

File hashes

Hashes for jumpssh-1.6.5.tar.gz
Algorithm Hash digest
SHA256 c47ba428549a3d78de8f1a903396cb96674638f63f3f672d81aa11224bec68bd
MD5 ef788cea350eb6281a55ad469dda535f
BLAKE2b-256 ddccf9c892b7c22de44c29911be13d5c6e570a064f7603ac98d49a3ed679c176

See more details on using hashes here.

File details

Details for the file jumpssh-1.6.5-py3-none-any.whl.

File metadata

  • Download URL: jumpssh-1.6.5-py3-none-any.whl
  • Upload date:
  • Size: 32.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.20.1 setuptools/38.5.2 requests-toolbelt/0.8.0 tqdm/4.23.4 CPython/3.7.1

File hashes

Hashes for jumpssh-1.6.5-py3-none-any.whl
Algorithm Hash digest
SHA256 65a5224c5c7a41e72446675dcf09579629632b68ca11a703141254b0cc7c8aff
MD5 6517e2b91be9712cca93d5c6df5b1204
BLAKE2b-256 f6d1f168740791269c4e3944e229a617a276193e64750f370c7d3ef0a15dce1d

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