Skip to main content

OpenSSH python wrapper

Project description

Under some circumstances simple wrapper around OpenSSH ssh command-line utility seems more preferable than paramiko machinery.

This project proposes yet another hopefully thin wrapper around ssh to execute commands on remote servers. All you need thereis to make sure that OpenSSH client and Python interpreter are installed, and then install openssh-wrapper package.

Usage sample

Simple command execution

>>> from openssh_wrapper import SSHConnection
>>> conn = SSHConnection('localhost', 'root')
>>> ret = conn.run('whoami')
>>> print ret
command: whoami
stdout: root
stderr:
returncode: 0
>>> ret.command
'whoami'
>>> ret.stdout
'root'
>>> ret.stderr
''
>>> ret.returncode
0

If python interpreter is installed on a remote machine, you can also run pieces of python code remotely. The same is true for any other interpreter which can execute code from stdin

>>> ret = conn.run('whoami')
>>> print conn.run('print "Hello world"', interpreter='/usr/bin/python').stdout
Hello world

Yet another userful run method option is forward_ssh_agent (the feature which paramiko doesn’t yet have). Suppose you have access as support to foobar server while root@localhost does not, so you can take advantage of SSH agent forwarding

$ eval `ssh-agent`
Agent pid 5272
$ ssh-add
Identity added: /home/me/.ssh/id_rsa (/home/,e/.ssh/id_rsa)
$ python
>>> conn = SSHConnection('localhost', 'root')
>>> print conn.run('ssh support@foobar "whoami"', forward_ssh_agent=True).stdout
support

And finally there is a sample which shows how to copy a file from local to remote machine

>>> fd = open('test.txt', 'w')
>>> fd.write('Hello world')
>>> fd.close()
>>> from openssh_wrapper import SSHConnection
>>> conn = SSHConnection('localhost', 'root')
>>> conn.scp('test.txt', target='/tmp')
>>> print conn.run('cat /tmp/test.txt').stdout
Hello world

Project details


Download files

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

Source Distribution

openssh-wrapper-0.2.1.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file openssh-wrapper-0.2.1.tar.gz.

File metadata

File hashes

Hashes for openssh-wrapper-0.2.1.tar.gz
Algorithm Hash digest
SHA256 e772193e6ce30008e1b204ae217b4ab5864a15f22dfcd1afebacb816850b6857
MD5 382bfe3ed920754d088e610c2230257c
BLAKE2b-256 1bb80e2a73eba6a579f0a55ab6c8ad3d4a1d0fb9f1d308686514c98af4034653

See more details on using hashes here.

Supported by

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