Issues trying to SSH into a fresh EC2 instance with Paramiko

后端 未结 5 2368
执念已碎
执念已碎 2021-02-14 14:28

I\'m working on a script that spins up a fresh EC2 instance with boto and uses the Paramiko SSH client to execute remote commands on the instance. For whatever reason, the Param

5条回答
  •  悲&欢浪女
    2021-02-14 14:36

    The way to check it's ssh available is to make sure its two status checks both passes. On web UI it looks like this:

    And using boto3 (the original question used boto but it was 5 years ago), we can do:

    session = boto3.Session(...)
    client = session.client('ec2')
    res = client.run_instances(...) # launch instance
    instance_id = res['Instances'][0]['InstanceId']
    
    while True:
        statuses = client.describe_instance_status(InstanceIds=[instance_id])
        status = statuses['InstanceStatuses'][0]
        if status['InstanceStatus']['Status'] == 'ok' \
                and status['SystemStatus']['Status'] == 'ok':
            break
        print '.'
        time.sleep(5)
    print "Instance is running, you are ready to ssh to it"
    

提交回复
热议问题