Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from pssh.clients.miko.single import SSHClient as MikoClient
from pssh.utils import load_private_key
import os
import sys
from datetime import datetime
from pssh.clients.native.single import SSHClient
user = sys.argv[1]
host = sys.argv[2]
pkey_path = os.path.expanduser("~/.ssh/id_rsa")
cmd = "echo 'hello world'"
paramiko_times = []
miko_client = MikoClient(host, user=user, pkey=load_private_key(pkey_path))
times = []
for i in range(9):
start = datetime.now()
channel, _, stdout, stderr, _ = miko_client.exec_command(cmd)
stdout = list(stdout)
channel.close()
total = datetime.now() - start
paramiko_times.append(total)
assert stdout[0].strip() == "hello world"
psshrun_times = []
client = SSHClient(host, user=user, pkey=pkey_path)
for i in range(7):
start = datetime.now()
res = client.run_command(cmd)
channel = res[0]
stdout = list(res[2])
client.close_channel(channel)
_run = datetime.now() - start
assert stdout[0].strip() == "hello world"
psshrun_times.append(_run)
psshopen_times = []
psshcmd_times = []
psshtotal_times = []
psshclose_times = []
for i in range(7):
start = datetime.now()
channel = client.open_session()
chan_open = datetime.now() - start
start = datetime.now()
client.execute(cmd, channel=channel)
execute = datetime.now() - start
start = datetime.now()
stdout = "\n".join(client.read_output(channel))
_read = datetime.now() - start
start = datetime.now()
client.close_channel(channel)
psshclose = datetime.now() - start
assert stdout.strip() == "hello world"
psshopen_times.append(chan_open)
psshcmd_times.append(execute)
psshtotal_times.append(_read)
psshclose_times.append(psshclose)
def fmt_times(name, _times):
# Convert to ms
times = [t.total_seconds()*1000 for t in _times]
return "{:<20} {:8.2f} {:8.2f} {:8.2f}".format(
name, min(times), max(times), sum(times) / len(times)
)
print(" min [ms] max [ms] avg [ms]")
print(fmt_times("paramiko", paramiko_times))
print(fmt_times("pssh run_command", psshrun_times))
print(fmt_times("pssh execute", psshtotal_times))
print(fmt_times(" open", psshopen_times))
print(fmt_times(" cmd", psshcmd_times))
print(fmt_times(" close", psshclose_times))
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from paramiko import SSHClient as MikoClient, MissingHostKeyPolicy
from paramiko.rsakey import RSAKey
from ssh2.session import Session
import os
import sys
from datetime import datetime
import socket
user = sys.argv[1]
host = sys.argv[2]
pkey_path = os.path.expanduser("~/.ssh/id_rsa")
pub_path = pkey_path + '.pub'
cmd = "echo 'hello world'"
def connect_ssh2():
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, 22))
s = Session()
s.handshake(sock)
s.userauth_publickey_fromfile(
user, pub_path, pkey_path, '')
return s
ssh2_session = connect_ssh2()
paramiko_times = []
miko_client = MikoClient()
miko_client.set_missing_host_key_policy(MissingHostKeyPolicy())
miko_client.connect(host, username=user, pkey=RSAKey.from_private_key(open(pkey_path, 'rb')))
times = []
for i in range(9):
start = datetime.now()
chan = miko_client.get_transport().open_session()
chan.exec_command(cmd)
stdout = list(chan.makefile('rb'))
chan.close()
total = datetime.now() - start
paramiko_times.append(total)
assert stdout[0].strip() == "hello world"
ssh2run_times = []
for i in range(7):
all_data = ""
start = datetime.now()
chan = ssh2_session.open_session()
chan.execute(cmd)
size, data = chan.read()
while size > 0:
all_data += data
size, data = chan.read()
chan.close()
_run = datetime.now() - start
assert all_data.strip() == "hello world"
ssh2run_times.append(_run)
ssh2open_times = []
ssh2cmd_times = []
ssh2total_times = []
ssh2close_times = []
for i in range(7):
all_data = ""
start = datetime.now()
chan = ssh2_session.open_session()
chan_open = datetime.now() - start
start = datetime.now()
chan.execute(cmd)
execute = datetime.now() - start
start = datetime.now()
size, data = chan.read()
while size > 0:
all_data += data
size, data = chan.read()
_read = datetime.now() - start
start = datetime.now()
chan.close()
ssh2close = datetime.now() - start
assert all_data.strip() == "hello world"
ssh2open_times.append(chan_open)
ssh2cmd_times.append(execute)
ssh2total_times.append(_read)
ssh2close_times.append(ssh2close)
def fmt_times(name, _times):
# Convert to ms
times = [t.total_seconds()*1000 for t in _times]
return "{:<20} {:8.2f} {:8.2f} {:8.2f}".format(
name, min(times), max(times), sum(times) / len(times)
)
print(" min [ms] max [ms] avg [ms]")
print(fmt_times("paramiko", paramiko_times))
print(fmt_times("ssh2 run_command", ssh2run_times))
print(fmt_times("ssh2 execute", ssh2total_times))
print(fmt_times(" open", ssh2open_times))
print(fmt_times(" cmd", ssh2cmd_times))
print(fmt_times(" close", ssh2close_times))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.