在Python中获取Google Compute Engine实例IP地址的最简单方法是什么? [英] What's the simplest way to get the IP address of a Google Compute Engine Instance in Python?
本文介绍了在Python中获取Google Compute Engine实例IP地址的最简单方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我知道实例的区域和名称,那么获取实例的公共IP地址的最简单方法是什么?
If I know the region and name of an instance, what's the simplest way to get the instance's public IP address?
def get_public_ip(region, instance_name):
# ???
推荐答案
使用gcutil:
$ gcutil listinstances --columns=external-ip
或使用python api networkInterfaces
结构如下:
or using python api networkInterfaces
structure looks like:
u'networkInterfaces':[
{
u'accessConfigs':[
{
u'kind':u'compute#accessConfig',
u'type':u'ONE_TO_ONE_NAT',
u'name':u'External NAT',
u'natIP':u'xxx.xxx.xxx.xxx'
}
],
u'networkIP':u'10.xxx.xxx.xxx',
u'network': u'https://www.googleapis.com/compute/v1/projects/<my-project-id>/global/networks/<network-name>',
u'name':u'nic0'
}
]
因此您可以使用此列表实例示例像这样:
so you can use this Listing Instances example to do something like:
for instance in instances:
print instance['networkInterfaces'][0]['accessConfigs'][0]['natIP']
这篇关于在Python中获取Google Compute Engine实例IP地址的最简单方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文