Skip to content

Instantly share code, notes, and snippets.

@tthtlc
Created September 29, 2018 06:48
Show Gist options
  • Save tthtlc/b98aa6784e304619225b3a440f5fe141 to your computer and use it in GitHub Desktop.
Save tthtlc/b98aa6784e304619225b3a440f5fe141 to your computer and use it in GitHub Desktop.
how to use python + ssh command to run command remotely
import subprocess
import sys
HOST="[email protected]"
# Ports are handled in ~/.ssh/config since we use OpenSSH
COMMAND="uname -a"
COMMAND="ls -axpt"
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment