Skip to content

Instantly share code, notes, and snippets.

@kanaka
Created September 16, 2022 15:05
Show Gist options
  • Save kanaka/ea5849c95f2f74672b91419c239acd55 to your computer and use it in GitHub Desktop.
Save kanaka/ea5849c95f2f74672b91419c239acd55 to your computer and use it in GitHub Desktop.
sshping - create ssh connect and measure character typing latency (send single character until it appears)
#!/usr/bin/env python
# You may use this under an MIT license
from time import time, sleep
from signal import signal, SIGINT
import subprocess
import sys
def write_read(p):
a = time()
p.stdin.write(b"z")
assert p.stdout.read(1) == b"z"
return round(1000 * (time() - a), 1)
host = sys.argv[1]
p = subprocess.Popen(['ssh', host, 'cat'],
stdin=subprocess.PIPE, stdout=subprocess.PIPE, bufsize=0)
times = []
def finish(*args):
avg1 = round(sum(times) / len(times), 1)
avg2 = round(sum(times[1:]) / (len(times)-1), 1)
print("\nAverage (including first): %s ms" % avg1)
print("Average (not including first): %s ms" % avg2)
sys.exit(0)
signal(SIGINT, finish)
first = True
while True:
ms = write_read(p)
times.append(ms)
if len(times) == 0:
note = " (including connect time)"
else:
note = ""
print("1 char to/from %s: %s ms%s" % (host, ms, note))
sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment