stuck with status check of amazon ec2 instance

2019-07-04 06:42发布

I am using Python to launch an ec2 instance, after I get "running" state of my instance, I am trying to SCP a shell script and run it via ssh.

I am getting the following error

"ssh: connect to host ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com port 22: Connection refused"

When I check in the console, the Status check is "Initializing", once It changes "2/2 checks passed", I am able to ssh or run any script.

Is there any way I can get the "status check" via python boto API?

I am using Python 2.7.5+, boto 2.19.0

Thanks in advance.

2条回答
一纸荒年 Trace。
2楼-- · 2019-07-04 06:50

Lazy way

import boto.ec2

for region in boto.ec2.regions():
        connection = boto.ec2.connect_to_region(region.name, 
            aws_access_key_id = '<aws access key>', aws_secret_access_key = '<aws secret key>')
        existing_instances = connection.get_all_instance_status()
        print 'Listing instances from region ' + region.name
        for instance in existing_instances:
            print instance.system_status.status + '/' + instance.instance_status.status
查看更多
Bombasti
3楼-- · 2019-07-04 07:09

Simple way is to check the port 22 of the newly created instance is reachable or not by using socket module

import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
     s.connect(('hostname', 22))
     print "Port 22 reachable"
except socket.error as e:
     print "Error on connect: %s" % e
s.close()

When you will able to reach the port 22 then you can invoke ssh to it.

查看更多
登录 后发表回答