Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the simplest way to SSH using Python?

How can I simply SSH to a remote server from a local Python (3.0) script, supply a login/password, execute a command and print the output to the Python console?

I would rather not use any large external library or install anything on the remote server.

like image 697
Christopher Tokar Avatar asked Aug 05 '09 14:08

Christopher Tokar


People also ask

How do you SSH in Python?

SSH is widely used by network administrators for managing systems and applications remotely, allowing them to log in to another computer over a network, execute commands and move files from one computer to another. In python SSH is implemented by using the python library called fabric.

How do I use Paramiko in Python?

A Paramiko SSH Example: Connect to Your Server Using a Password. This section shows you how to authenticate to a remote server with a username and password. To begin, create a new file named first_experiment.py and add the contents of the example file. Ensure that you update the file with your own Linode's details.


2 Answers

You can code it yourself using Paramiko, as suggested above. Alternatively, you can look into Fabric, a python application for doing all the things you asked about:

Fabric is a Python library and command-line tool designed to streamline deploying applications or performing system administration tasks via the SSH protocol. It provides tools for running arbitrary shell commands (either as a normal login user, or via sudo), uploading and downloading files, and so forth.

I think this fits your needs. It is also not a large library and requires no server installation, although it does have dependencies on paramiko and pycrypt that require installation on the client.

The app used to be here. It can now be found here.

* The official, canonical repository is git.fabfile.org * The official Github mirror is GitHub/bitprophet/fabric 

There are several good articles on it, though you should be careful because it has changed in the last six months:

Deploying Django with Fabric

Tools of the Modern Python Hacker: Virtualenv, Fabric and Pip

Simple & Easy Deployment with Fabric and Virtualenv


Later: Fabric no longer requires paramiko to install:

$ pip install fabric Downloading/unpacking fabric   Downloading Fabric-1.4.2.tar.gz (182Kb): 182Kb downloaded   Running setup.py egg_info for package fabric     warning: no previously-included files matching '*' found under directory 'docs/_build'     warning: no files found matching 'fabfile.py' Downloading/unpacking ssh>=1.7.14 (from fabric)   Downloading ssh-1.7.14.tar.gz (794Kb): 794Kb downloaded   Running setup.py egg_info for package ssh Downloading/unpacking pycrypto>=2.1,!=2.4 (from ssh>=1.7.14->fabric)   Downloading pycrypto-2.6.tar.gz (443Kb): 443Kb downloaded   Running setup.py egg_info for package pycrypto Installing collected packages: fabric, ssh, pycrypto   Running setup.py install for fabric     warning: no previously-included files matching '*' found under directory 'docs/_build'     warning: no files found matching 'fabfile.py'     Installing fab script to /home/hbrown/.virtualenvs/fabric-test/bin   Running setup.py install for ssh   Running setup.py install for pycrypto ... Successfully installed fabric ssh pycrypto Cleaning up... 

This is mostly cosmetic, however: ssh is a fork of paramiko, the maintainer for both libraries is the same (Jeff Forcier, also the author of Fabric), and the maintainer has plans to reunite paramiko and ssh under the name paramiko. (This correction via pbanka.)

like image 98
hughdbrown Avatar answered Sep 27 '22 16:09

hughdbrown


I haven't tried it, but this pysftp module might help, which in turn uses paramiko. I believe everything is client-side.

The interesting command is probably .execute() which executes an arbitrary command on the remote machine. (The module also features .get() and .put methods which allude more to its FTP character).

UPDATE:

I've re-written the answer after the blog post I originally linked to is not available anymore. Some of the comments that refer to the old version of this answer will now look weird.

like image 24
ThomasH Avatar answered Sep 27 '22 17:09

ThomasH