2014-02-07 3 views
0

인스턴스의 지역 및 이름을 알고있는 경우 인스턴스의 공용 IP 주소를 가져 오는 가장 간단한 방법은 무엇입니까?Python에서 Google Compute Engine 인스턴스의 IP 주소를 가져 오는 가장 간단한 방법은 무엇입니까?

def get_public_ip(region, instance_name): 
    # ??? 
+0

https://developers.google.com/compute/docs/instances-and-network을 종료 컴퓨팅 엔진 앞에 나열된 외부 IP을 반환 #ipaddresses –

+0

gcutil에 쉘 호출을하는 것이 가장 쉬운 방법이라고 생각하십니까? Python API를 사용하는 것이 더 쉬워지기를 바랬습니다. –

+0

나는 당신이 이미 알고있는 것을 알아 내려고 애 썼습니다. 당신은 당신이 시도한 것에 대해, 또는 당신이 이미 한 연구에 대해 많이 말하지 않았습니다. –

답변

2

사용 gcutil :

$ gcutil listinstances --columns=external-ip 

또는 같은 networkInterfaces 구조가 보이는 파이썬 API를 사용하여 :

: 당신이 그런 짓을 할 this Listing Instances example을 사용할 수 있도록

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' 
    } 

]

for instance in instances: 
    print instance['networkInterfaces'][0]['accessConfigs'][0]['natIP'] 
0

이 시도 :

credentials = GoogleCredentials.get_application_default() 
api = build('compute', 'v1', credentials=credentials) 

response = api.instances().get(project=project_id, zone=region, instance=instance_name).execute() 

ip = response['networkInterfaces'][0]['accessConfigs'][0]['natIP'] 

관련 문제