Last active
September 1, 2021 12:02
-
-
Save cloudnull/36dd43d0ad1cf4fb05defb8fda02129c to your computer and use it in GitHub Desktop.
testiing the performance pylibssh from the ansible team
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
from __future__ import print_function | |
import time | |
from pylibsshext.errors import LibsshSessionException | |
from pylibsshext.session import Session | |
host = '172.16.27.26' | |
user = 'centos' | |
password = 'secrete' | |
key_file = "/home/centos/.ssh/id_rsa" | |
with open(key_file, 'rb') as f: | |
key_data = f.read() | |
ssh = Session() | |
try: | |
ssh.connect( | |
host=host, | |
user=user, | |
timeout=30, | |
port=22, | |
private_key=key_data | |
) | |
except LibsshSessionException as ssh_exc: | |
print(f'Failed to connect to {host} over SSH: {ssh_exc!s}') | |
print(f'Connection status: {ssh.is_connected}') | |
class PerfCheck: | |
def __init__(self, notice=""): | |
self.tic = 0 | |
self.notice = notice | |
def __enter__(self): | |
self.toc = time.perf_counter() | |
def __exit__(self, *args, **kwargs): | |
tic = time.perf_counter() | |
print(f"Elapsed {self.notice} time: {tic - self.toc:0.4f} seconds") | |
with PerfCheck(notice="total"): | |
with PerfCheck(notice="channel create"): | |
ssh_channel = ssh.new_channel() | |
try: | |
with PerfCheck(notice="execution"): | |
cmd_resp = ssh_channel.write(b'ls') | |
print(f"Command object: {cmd_resp}") | |
with PerfCheck(notice="responce check"): | |
for item in ["stdout", "stderr", "returncode"]: | |
if hasattr(cmd_resp, item): | |
print('{key}:\n{data}\n'.format(key=item, data=getattr(cmd_resp, item))) | |
finally: | |
ssh_channel.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment