Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python检查重新启动后AWS实例是否启动_Python_Python 3.x_Amazon Web Services_Boto3_Aws Ec2 - Fatal编程技术网

使用python检查重新启动后AWS实例是否启动

使用python检查重新启动后AWS实例是否启动,python,python-3.x,amazon-web-services,boto3,aws-ec2,Python,Python 3.x,Amazon Web Services,Boto3,Aws Ec2,是否有一种方法可以检查AWS实例是否最终在python中使用boto3或其他方式出现。运行状态不区分重新启动和最终启动阶段。如果您只想检查远程端口是否打开,可以使用内置软件包 下面是等待远程端口打开的快速修改: import socket import time def wait_for_socket(host, port, retries, retry_delay=30): retry_count = 0 while retry_count <= retries:

是否有一种方法可以检查AWS实例是否最终在python中使用boto3或其他方式出现。运行状态不区分重新启动和最终启动阶段。

如果您只想检查远程端口是否打开,可以使用内置软件包

下面是等待远程端口打开的快速修改:

import socket
import time

def wait_for_socket(host, port, retries, retry_delay=30):
    retry_count = 0
    while retry_count <= retries:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((host, port))
        sock.close()
        if result == 0:
            print "Port is open"
            break
        else:
            print "Port is not open, retrying..."
            time.sleep(retry_delay)

如果只想检查远程端口是否打开,可以使用内置包

下面是等待远程端口打开的快速修改:

import socket
import time

def wait_for_socket(host, port, retries, retry_delay=30):
    retry_count = 0
    while retry_count <= retries:
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((host, port))
        sock.close()
        if result == 0:
            print "Port is open"
            break
        else:
            print "Port is not open, retrying..."
            time.sleep(retry_delay)

所有信息都可以在boto3文档中找到

这将显示实例的所有信息

import boto3
reservations = boto3.client("ec2").describe_instances()["Reservations"]
for reservation in reservations:
  for each in reservation["Instances"]:
    print " instance-id{} :  {}".format(each["InstanceId"], each["State"]["Name"])

# or use describe_instance_status, much simpler query
instances = boto3.client("ec2").describe_instance_status()
for each in instances["InstanceStatuses"]: 
  print " instance-id{} :  {}".format(each["InstanceId"], each["InstanceState"]["Name"])
从文件中:

State (dict) --

The current state of the instance.

Code (integer) --

The low byte represents the state. The high byte is an opaque internal value and should be ignored.

0 : pending
16 : running
32 : shutting-down
48 : terminated
64 : stopping
80 : stopped
Name (string) --

The current state of the instance.
事实上,文档中没有显示重新启动的代码状态。我不能在我自己的EC2实例上真正测试它,因为在我重新启动之后,这些实例似乎重新启动得太快,以至于AWS控制台没有机会显示重新启动状态

然而,

以下是可能导致实例状态的问题示例 检查失败:

系统状态检查失败

网络或启动配置不正确

耗尽的记忆

损坏的文件系统

不兼容内核


所有信息都可以在boto3文档中找到

这将显示实例的所有信息

import boto3
reservations = boto3.client("ec2").describe_instances()["Reservations"]
for reservation in reservations:
  for each in reservation["Instances"]:
    print " instance-id{} :  {}".format(each["InstanceId"], each["State"]["Name"])

# or use describe_instance_status, much simpler query
instances = boto3.client("ec2").describe_instance_status()
for each in instances["InstanceStatuses"]: 
  print " instance-id{} :  {}".format(each["InstanceId"], each["InstanceState"]["Name"])
从文件中:

State (dict) --

The current state of the instance.

Code (integer) --

The low byte represents the state. The high byte is an opaque internal value and should be ignored.

0 : pending
16 : running
32 : shutting-down
48 : terminated
64 : stopping
80 : stopped
Name (string) --

The current state of the instance.
事实上,文档中没有显示重新启动的代码状态。我不能在我自己的EC2实例上真正测试它,因为在我重新启动之后,这些实例似乎重新启动得太快,以至于AWS控制台没有机会显示重新启动状态

然而,

以下是可能导致实例状态的问题示例 检查失败:

系统状态检查失败

网络或启动配置不正确

耗尽的记忆

损坏的文件系统

不兼容内核

你也可以在boto3或任何合适的地方使用

import boto3

instance_id = '0-12345abcde'

client = boto3.client('ec2')
client.reboot_instances(InstanceIds=[instance_id])

waiter = client.get_waiter('instance_status_ok')
waiter.wait(InstanceIds=[instance_id])

print("The instance now has a status of 'ok'!")
你也可以在boto3或任何合适的地方使用

import boto3

instance_id = '0-12345abcde'

client = boto3.client('ec2')
client.reboot_instances(InstanceIds=[instance_id])

waiter = client.get_waiter('instance_status_ok')
waiter.wait(InstanceIds=[instance_id])

print("The instance now has a status of 'ok'!")

你对up的定义是什么?up在所有进程中都已启动,如果我通过putty重新启动,应该启动,最后up将意味着允许我再次登录,因为你的意思是端口22已打开?是的。如果boto3中有一些API可以直接告诉我有关实例的信息,那就更好了,你可以在这部分使用socket,而不是BOTOW。你对up的定义是什么?up在所有进程中都已启动,如果我通过putty重新启动,最终up将意味着允许我重新登录。你是说端口22已打开?是的。如果boto3中有一些API可以直接告诉我有关实例的信息,那就更好了,你可以使用socket来代替刚刚实现的botoI,不是所有的节点都有一个公共ip。在那种情况下我能做什么?我希望只给其中一个节点提供公共ip,该节点将具有对其他节点的内部访问权限。将此脚本放入AWS Lambda,该脚本与VPC子网交互。我刚刚意识到,并非所有节点都有公共ip。在那种情况下我能做什么?我希望只将公共ip提供给其中一个节点,该节点将具有对其他节点的内部访问权限。将此脚本放入AWS Lambda,该脚本与VPC子网交互。