Skip to content

Instantly share code, notes, and snippets.

@direvius
Last active April 13, 2016 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save direvius/cfe8d0694a6246ecb21f to your computer and use it in GitHub Desktop.
Save direvius/cfe8d0694a6246ecb21f to your computer and use it in GitHub Desktop.
test paramiko lib
import logging
from paramiko import SSHClient, AutoAddPolicy
logging.basicConfig(level=logging.DEBUG,
format='%(asctime)s %(levelname)s %(message)s')
logging.getLogger("paramiko.transport").setLevel(logging.DEBUG)
logger = logging.getLogger(__name__)
class SecuredShell(object):
def __init__(self, host, port, username, timeout):
self.host = host
self.port = port
self.username = username
self.timeout = timeout
def __connect(self):
logger.debug("Opening SSH connection to {host}:{port}".format(
host=self.host, port=self.port))
client = SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(AutoAddPolicy())
client.connect(
self.host,
port=self.port,
username=self.username,
timeout=self.timeout,
)
return client
def execute(self, cmd):
with self.__connect() as client:
_, stdout, stderr = client.exec_command(cmd)
output = stdout.read()
errors = stderr.read()
return output, errors
ssh = SecuredShell("example.org", 22, "my-username", 10)
print(ssh.execute("ls -l"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment