【问题标题】:stuck with status check of amazon ec2 instance卡住亚马逊 ec2 实例的状态检查
【发布时间】:2019-09-26 01:39:25
【问题描述】:

我正在使用 Python 启动 ec2 instance,在我获得实例的 "running" 状态后,我正在尝试 SCP 一个 shell 脚本并通过 ssh 运行它。

我收到以下错误

“ssh:连接到主机 ec2-xx-xxx-xxx-xxx.compute-1.amazonaws.com 端口 22:连接被拒绝”

当我检查控制台时,状态检查是"Initializing",一旦它更改"2/2 checks passed",我就可以ssh 或运行任何脚本。

有什么方法可以通过python boto API 获得“状态检查”?

我正在使用 Python 2.7.5+, 博托 2.19.0

提前致谢。

【问题讨论】:

  • 您好,您找到解决此问题的方法了吗?我正在与非常相似的事情作斗争。

标签: python amazon-web-services ssh amazon-ec2


【解决方案1】:

简单的方法是使用socket模块检查新创建实例的22端口是否可达

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()

当您能够访问端口 22 时,您可以调用 ssh 到它。

【讨论】:

    【解决方案2】:

    懒惰的方式

    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
    

    【讨论】:

    • 嗨 Felipe - 您是否熟悉用户在上面遇到的错误?我目前正在经历它,想知道您是否有任何建议。我已经使用了您推荐的方法,但仍然出现连接错误。
    • 您应该查看 Boto 服务员,对于这种特定情况,InstanceStatusOk 服务员。 boto3.readthedocs.io/en/latest/reference/services/…
    • 谢谢费利佩!会看的。
    猜你喜欢
    • 2010-12-28
    • 2014-10-08
    • 2014-03-30
    • 2020-05-12
    • 2014-09-04
    • 2013-09-28
    • 2013-12-23
    • 2016-04-27
    • 1970-01-01
    相关资源
    最近更新 更多