Skip to content

Instantly share code, notes, and snippets.

@ryanbekabe
Created August 11, 2020 10:28
Show Gist options
  • Save ryanbekabe/4c6c1cc05ee9bc7ab5fd8c097274218a to your computer and use it in GitHub Desktop.
Save ryanbekabe/4c6c1cc05ee9bc7ab5fd8c097274218a to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# https://gist.github.com/bortzmeyer/1284249
# https://www.cnpython.com/qa/53070
# 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="[email protected]"
#cloud0305
# Ports are handled in ~/.ssh/config since we use OpenSSH
#COMMAND="uname -a"
#COMMAND="ls -lath"
COMMAND="lsof -i tcp:22"
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