如何使用Boto获取启动实例的IP地址

8 投票
1 回答
9676 浏览
提问于 2025-04-18 11:58

我正在使用boto在OpenStack中启动一个实例。

myinstance = conn.run_instances('ami-0000007d',min_count=1,max_count=1, instance_type = 'm1.small')

newmachine=myinstance.instances[0]

newMachine里有关于这个启动实例的信息。我尝试过

vars(newmachine)

但是ip_address和private_ip_address这两个变量是空的。我该如何获取这个启动实例的ip_address呢?

1 个回答

13

在实例进入运行状态之前,一直刷新这个值。等到它进入运行状态时,IP地址就应该出现了(在实例还没运行之前,你是无法使用这个IP地址的)。

reservation = conn.run_instances(...)

instance = reservation.instances[0]

while instance.update() != "running":
    time.sleep(5)  # Run this in a green thread, ideally

print instance.ip_address

撰写回答