Skip to content

Instantly share code, notes, and snippets.

@SavvyGuard
Created July 30, 2013 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SavvyGuard/6109123 to your computer and use it in GitHub Desktop.
Save SavvyGuard/6109123 to your computer and use it in GitHub Desktop.
Wait for SSH Connection to become ready with progress bar when trying to connect to Amazon EC2 server
import subprocess
import boto.ec2
import sys
def _progressBar(state):
progress_bar = '=' * state
sys.stdout.write('\r'+progress_bar)
sys.stdout.flush()
# recursive function to wait until the ssh connection is ready
def _awaitAWSInstanceSSHReady(instance, loops):
time_interval = 5
key_path = '.uwa/'+AWS_REGION + '/'+AWS_SSH_KEY_NAME + '.pem'
fnull = open(os.devnull, 'w')
Process = subprocess.Popen(["ssh", "-i", key_path, "-o", "StrictHostKeyChecking no", "ec2-user@"+instance.public_dns_name, "ls"], stdin = fnull, stdout = fnull, stderr = fnull)
Process.poll()
while (Process.returncode == None):
loops += 1
_progressBar(loops)
time.sleep(time_interval)
Process.poll()
if (Process.returncode == 0):
return True
elif (Process.returncode == 255):
_progressBar(loops)
time.sleep(time_interval)
_awaitAWSInstanceSSHReady(instance, loops + 1)
else:
raise Exception('SSH weird error', instance.public_dns_name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment