Post SFTP in Python: Really Simple SSH | The best of Zeth
SFTP in Python: Really Simple SSH
This entry was posted in Python by zeth. Bookmark the permalink.ssh.py provides three common SSH operations, get, put and execute. It is a high-level abstraction upon Paramiko.
I wrote it yesterday for my own needs, so it is still very much in the beta stage. Any improvements or comments gratefully accepted.
In short, it works as follows:
import ssh s = ssh.Connection('example.com') s.put('hello.txt') s.get('goodbye.txt') s.execute('du -h --max-depth=0') s.close()That is it, in the rest of this post, I walk through this line by line.
Installation
First, we need to install paramiko, if you don't have it already.
On Gentoo Linux:
emerge paramiko
On Ubuntu/Debian and so on:
apt-get install python-paramiko
If you want to use Python's easy_install then:
easy_install paramiko
Secondly, you need to grab the ssh.py module, grab it from my code-page, and save it as ssh.py.
Connecting to a remote server
To play with the script interactively, you need to start Python:
python
Now, import the ssh module:
import sshNext we need to initiate the connection. If your username is the same on both systems, and you have set up ssh-keys, then all you need to do is:
s = ssh.Connection('example.com')Connection supports the following options:
host The Hostname of the remote machine. username Your username at the remote machine. private_key Your private key file. password Your password at the remote machine. port The SSH port of the remote machine. The host is essential of course. Port defaults to 22. The username defaults to the username you are currently using on the local machine.
You need to use one of the authentication methods, a private key or a password. If you don't specify anything, then ssh.Connection will attempt to use a private_key at ~/.ssh/id_rsa or ~/.ssh/id_dsa.
So to specify a username and password, you can do it like this:
s = ssh.Connection(host = 'example.com', username = 'warrior', password = 'lennalenna')Of course, Python also allows you to use the order to specify the arguments, so the last example can be written as:
s = ssh.Connection('example.com', 'warrior', password = 'lennalenna')Operations
Once you have set up the connection, there are three methods you can use. Firstly, to send a file from the local machine, you can use put:
s.put('hello.txt')The above example copies a file called hello.txt from the current local working directory to the remote server. We can also be more explicit if we want:
s.put('/home/warrior/hello.txt', '/home/zombie/textfiles/report.txt')So the above example copies /home/warrior/hello.txt on the local server to /home/zombie/textfiles/report.txt on the remote server.
The second operation works in a similar way but in reverse:
s.get('hello.txt')get takes the file from the remote server to the local server, again we can be more explicit if we want:
s.get('/var/log/strange.log', '/home/warrior/serverlog.txt')The above example copies the strange.log from the server and saves it as serverlog.txt.
The last operation is execute, this executes a command on the remote server:
s.execute('ls -l')This returns the output as a Python list.
Closing the connection
You can do as many operations you like while the connection is open, but when you are finished, you need to close the connection between the local and remote machines. You do this with the close method:
s.close()There we go, that is all I needed to do with SSH. Please do let me know using the comments below if you have any problems using it.
If you import my module in your program and later find that you need more power or flexibility, you should be able to swap it out for the full paramiko with a minimum of fuss.
22 thoughts on “SFTP in Python: Really Simple SSH”