Created
September 29, 2018 06:48
-
-
Save tthtlc/b98aa6784e304619225b3a440f5fe141 to your computer and use it in GitHub Desktop.
how to use python + ssh command to run command remotely
This file contains hidden or 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
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