Saturday, April 20, 2013

Using Python and paramiko to move bits around

Automating the movement of files around a stack is one of those unglamorous jobs that just has to get done. It's certainly possible to do this in bash but Python makes it a little more easy and fun.

paramiko is an actively maintained Python module that assists with secure connections to remote hosts. This use cases favors the SFTPClient class which easily allows an app to connect to and put files on a remote machine. paramiko is LGPL and only requires Python 2.5+ and PyCrypto 2.1+.

It's probably best to create an account on the remote host with a very limited scope. That is, it only has permission to modify the directory which you intend to write to. Those creds will go in the Python script. Don't run this as root or your normal user account!

In this simple example, all files in a particular local directory will be recursively copied to a remote host. Enjoy!