Skip to content

Instantly share code, notes, and snippets.

@sati2013
Forked from bortzmeyer/gist:1284249
Last active December 11, 2015 23:58
Show Gist options
  • Save sati2013/4680442 to your computer and use it in GitHub Desktop.
Save sati2013/4680442 to your computer and use it in GitHub Desktop.
#!/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
import getpass
HOST="www.example.org"
PW = "getpass.getpass("sudo PW: ")
# WE'll probably want to use keys as well
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="/bin/echo %s | sudo -S uname -a" % PW
ssh = subprocess.Popen(["ssh","-t", "%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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment