Where Python meets a shell
SSH is great, but it’s only part of a story. More often than not, you deal with local processes. How do you cope with them in Python?
Of course, you can use what the “subprocess” provides. This is an option, yet it makes your code lengthy and less readable, compared to what you have in a shell. Or you can mix-match Python and shell scripts, leaving each language a niche it’s best suited to.
The best of both worlds is psh, a Python library ( https://pypi.python.org/pypi/psh) that makes Python feel like a shell when you work with external commands: from psh import sh with sh.du('/home/valesini/YandexCloud') | sh.sort('-nr’, _ok_statuses=(0, 2)) | sh.head('-n’, 3) as cmd: for line in cmd: print line assert cmd.status() == 0 The _ok_statuses is here so that psh doesn’t throw an exception when sort complains about head closing its file descriptors and exiting upon receiving the first three lines.
psh leverages Python magic methods, keyword arguments and other syntactic sugar to implement piping, I/O redirection and other shell-level concepts. The documentation ( http://konishchevdmitry.github.io/psh/
intro.html#working-with-ssh) even reveals how to execute remote commands with SSH, which leaves you an alternative route just in case Paramiko didn’t sound like a good idea to you.