Skip to content

Instantly share code, notes, and snippets.

@hellt
Last active September 18, 2023 08:21
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hellt/6e5b657de8e504b60b56db252e1204e6 to your computer and use it in GitHub Desktop.
Save hellt/6e5b657de8e504b60b56db252e1204e6 to your computer and use it in GitHub Desktop.
wait_for_ssh function continuously checks whether SSH transport is ready
import paramiko
import time
def wait_for_ssh_to_be_ready(host, port, timeout, retry_interval):
client = paramiko.client.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
retry_interval = float(retry_interval)
timeout = int(timeout)
timeout_start = time.time()
while time.time() < timeout_start + timeout:
time.sleep(retry_interval)
try:
client.connect(host, int(port), allow_agent=False,
look_for_keys=False)
except paramiko.ssh_exception.SSHException as e:
# socket is open, but not SSH service responded
if e.message == 'Error reading SSH protocol banner':
print(e)
continue
print('SSH transport is available!')
break
except paramiko.ssh_exception.NoValidConnectionsError as e:
print('SSH transport is not ready...')
continue
wait_for_ssh_to_be_ready('localhost', '8000', '20', '1')
@hellt
Copy link
Author

hellt commented Oct 31, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment