Skip to content

Instantly share code, notes, and snippets.

@BenjamenMeyer
Last active April 5, 2017 19:34
Show Gist options
  • Save BenjamenMeyer/176f7b88ca8cbf8f6bf809288057e82c to your computer and use it in GitHub Desktop.
Save BenjamenMeyer/176f7b88ca8cbf8f6bf809288057e82c to your computer and use it in GitHub Desktop.
paramiko directly vs sshaolin wrapper
from paramiko.client import (
SSHClient,
WarningPolicy,
)
from paramiko.ssh_exception import *
host = "<host>"
user = "root"
password = "<password>"
filename = "f04"
client = SSHClient()
client.set_missing_host_key_policy(WarningPolicy())
client.connect(host, username=user, password=password)
# 4 * 1024 * 1024 = 4MiB
max_memory_buf_size = 4 * 1024 * 1024
# 4 * 1024 = 4KiB
max_ready_chunk_size = 4 * 1024
print("Test command failure")
try:
chan_client = client.get_transport().open_session()
chan_client.exec_command('mount /dev/alibaba /dev/null')
# -1 = no limit
stderr = chan_client.makefile('r', -1)
stdout = chan_client.makefile_stderr('r', -1)
print("Standard Out:")
print(stdout.read())
print("Standard Error:")
print(stderr.read())
while not chan_client.exit_status_ready():
pass
exit_code = chan_client.recv_exit_status()
chan_client.close()
print("Exit Code: {0}".format(exit_code))
except SSHException:
print("Caught exception")
sftp_client = client.open_sftp()
print("List Directory")
entries = sorted(
[
DIR_ENTRY
for DIR_ENTRY in sftp_client.listdir()
]
)
print("Directory Contents")
print("------------------------------------------------------")
print('\n'.join(entries))
if filename not in entries:
print("Upload File: {0}".format(filename))
sftp_client.put(filename, filename)
sftp_client.close()
client.close()
from sshaolin.client import SSHClient
host = "<host>"
user = "root"
password = "<password>"
filename = "f05"
client = SSHClient(hostname=host, username=user, password=password)
print("Test command failure")
try:
response = client.execute_command('mount /dev/alibaba /dev/null')
print("Standard Out:")
print(response.stdout)
print("Standard Error:")
print(response.stderr)
exit_code = response.exit_status
print("Exit Code: {0}".format(exit_code))
except SSHException:
print("Caught exception")
with client.create_sftp() as sftp_client:
print("List Directory")
entries = sorted(
[
DIR_ENTRY
for DIR_ENTRY in sftp_client.listdir()
]
)
print("Directory Contents")
print("------------------------------------------------------")
print('\n'.join(entries))
if filename not in entries:
print("Upload File: {0}".format(filename))
sftp_client.put(filename, filename)
sftp_client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment