A very simple thing that most developers want to do at one time or another is automate SSH. However, since OpenSSH reads the password from the terminal, this can make it a very annoying thing to automate. Indeed, searching google for a solution to this problem yields all sorts of bad answers (and very few good ones).
There are a number of ways to solve the problem in python other than a wrapper around the SSH command:
- Use public keys for authentication (this is a generally good practice for this sort of automation, but sometimes you can’t do this)
- Use paramiko to talk the SSH protocol directly
- Use pexpect‘s ssh wrapper to wrap openssh instead of rolling your own wrapper
However, if you can’t use external dependencies for some reason, this script is for you. It’s derived from this script and many other sources of information on the net, but I think it’s a bit simpler to use. It has been tested on Linux and OSX, on Python 2.7 and 3.4.
Let me know if you have problems with the script!