Skip to content

Instantly share code, notes, and snippets.

@mwilliamson

mwilliamson/test.py

Last active Dec 12, 2015
Embed
What would you like to do?
import time
from boto.ec2.connection import EC2Connection
from boto import ec2
import spur.ssh
import spur
key = '<your key here>'
secret = '<your secret here>'
instanceId = '<your instance id>'
region = ec2.get_region('us-west-2')
conn = EC2Connection(key, secret, region=region)
reservationList = conn.get_all_instances(instance_ids=[instanceId])
instance = reservationList[0].instances[0]
print(instance.public_dns_name == 'ec2-54-245-51-159.us-west-2.compute.amazonaws.com')
print('')
def attempt_connection():
shell = spur.SshShell(
hostname=instance.public_dns_name,
username='tony',
private_key_file='/home/tony/.ssh/grosinger-west',
missing_host_key=spur.ssh.MissingHostKey.accept
)
with shell:
print shell.run(["echo", "hello"]).output
def can_connect():
try:
print "Attempting connection..."
attempt_connection()
print "Success!"
return True
except spur.ssh.ConnectionError as error:
print "Failed"
print error.message
return False
timeout = 60 # One minute (adjust according to your level of patience)
start_time = time.time()
while not can_connect():
if time.time() - start_time > timeout:
raise Exception("I give up!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment