Created
July 30, 2013 00:31
-
-
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
This file contains 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
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