Created
October 13, 2011 13:42
-
Star
(127)
You must be signed in to star a gist -
Fork
(30)
You must be signed in to fork a gist
-
-
Save bortzmeyer/1284249 to your computer and use it in GitHub Desktop.
The only simple way to do SSH in Python today is to use subprocess + OpenSSH...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# All SSH libraries for Python are junk (2011-10-13). | |
# Too low-level (libssh2), too buggy (paramiko), too complicated | |
# (both), too poor in features (no use of the agent, for instance) | |
# Here is the right solution today: | |
import subprocess | |
import sys | |
HOST="www.example.org" | |
# Ports are handled in ~/.ssh/config since we use OpenSSH | |
COMMAND="uname -a" | |
ssh = subprocess.Popen(["ssh", "%s" % HOST, COMMAND], | |
shell=False, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.PIPE) | |
result = ssh.stdout.readlines() | |
if result == []: | |
error = ssh.stderr.readlines() | |
print >>sys.stderr, "ERROR: %s" % error | |
else: | |
print result | |
Amazing
Still populating top three google results when searching for 'python3 connect ssh and run script' :)
Still true today. Amazing how badly libraries like paramiko and fabric fail at such a simple task.
Still a valid chain but still finding it hard to reliably send password from Osx or linux
thank you
Can tell how amazing it is when this solution still works today.
Thanks a lot.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
SSH without third-party library
Here is my take on SSH without using third-party library. I leveraged a concept of
fork
system call in UNIX [1].Ref:
[1] https://en.wikipedia.org/wiki/Fork_(system_call)