1 回答

TA貢獻1776條經驗 獲得超12個贊
gcloud compute instances list
?您可以使用 Cloud Console命令或instances.list()
?方法列出項目中的所有實例?。
要以表格形式列出項目中的所有實例,請運行:
gcloud compute instances list
你會得到類似的東西:
NAME? ? ? ? ZONE? ? ? ? ? ?MACHINE_TYPE? ?PREEMPTIBLE? INTERNAL_IP? EXTERNAL_IP? ?STATUS
instance-1? us-central1-a? n1-standard-1? ? ? ? ? ? ? ?10.128.0.44? xx.xx.xxx.xx? RUNNING
instance-2? us-central1-b? n1-standard-1? ? ? ? ? ? ? ?10.128.0.49? xx.xx.xxx.xx? RUNNING
編輯1
正如您所提到的,aggregateList()是正確的,要獲取所需的信息,必須檢查 JSON 響應正文。
如果您需要某些特定字段,您可以檢查響應正文信息。
另外,您可以使用此代碼作為指南,我正在從實例中獲取所有信息。
from pprint import pprint
from googleapiclient import discovery
from oauth2client.client import GoogleCredentials
credentials = GoogleCredentials.get_application_default()
service = discovery.build('compute', 'v1', credentials=credentials)
# Project ID for this request.
project = "{Project-ID}"? # TODO: Update placeholder value.
request = service.instances().aggregatedList(project=project)
while request is not None:
? ? response = request.execute()
? ? instance = response.get('items', {})
? ? for instance in instance.values():
? ? ? for a in instance.get('instances', []):
? ? ? ? ? print(str(instance))
? ? request = service.instances().aggregatedList_next(previous_request=request, previous_response=response)
添加回答
舉報