使用前需安裝依賴的sdk模塊,能夠在阿里雲後臺查詢相關API使用方式:python
示例代碼:json
#! /usr/bin/env python # -\*- coding: utf-8 -\*- # \_\_author\_\_ = "shuke" # Date: 2017/9/26 import json, yaml try: from aliyunsdkcore.client import AcsClient from aliyunsdkcore import client from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest except ImportError as e: print("Error: ", e) exit() class AliCloudApi(object): """ 阿里雲API獲取數據 """ # 實例編號 hid = 1 # 全部region的主機 all\_instances\_dic = {} def \_\_init\_\_(self, key, secret, zone="cn-beijing"): self.key = key self.secret = secret self.zone = zone self.client = AcsClient(self.key, self.secret, self.zone) def instances\_count(self, page\_size=10): """ 返回頁碼數量 :param page\_size: :return: """ # 建立 request,並設置參數 request = DescribeInstancesRequest.DescribeInstancesRequest() request.set\_PageSize(page\_size) # 發起 API 請求並打印返回 response = self.client.do\_action\_with\_exception(request) ret = json.loads(response, encoding='utf-8') # 頁碼 total\_page, b = divmod(ret\['TotalCount'\], page\_size) if b: total\_page = total\_page + 1 return total\_page def queryinstances(self): """ 獲取各region的實例信息 :return: region下全部的實例信息 """ per\_region\_instances = \[\] try: total\_page = self.instances\_count() for page\_num in range(1, total\_page + 1): num = " 第%d頁 " % page\_num print(num.center(100, "\*")) # 建立 request,並設置參數 request = DescribeInstancesRequest.DescribeInstancesRequest() request.set\_PageNumber(page\_num) # 發起 API 請求並打印返回 response = self.client.do\_action\_with\_exception(request) ret = json.loads(response, encoding='utf-8') # print(json.dumps(ret), indent=4, ensure\_ascii=False)) instances = ret\['Instances'\]\['Instance'\] for item in instances: ele = {} ele\['ZoneId'\] = item\['ZoneId'\] ele\['InstanceId'\] = item\['InstanceId'\] ele\['InstanceType'\] = item\['InstanceType'\] ele\['HostName'\] = item\['HostName'\] ele\['InstanceName'\] = item\['InstanceName'\] # 公網IP if item\['PublicIpAddress'\]\['IpAddress'\]: ele\['PublicIpAddress'\] = item\['PublicIpAddress'\]\['IpAddress'\]\[0\] else: ele\['PublicIpAddress'\] = "" # 私網IP if item\['InnerIpAddress'\]\['IpAddress'\]: ele\['InnerIpAddress'\] = item\['InnerIpAddress'\]\['IpAddress'\]\[0\] else: ele\['InnerIpAddress'\] = item\['VpcAttributes'\]\['PrivateIpAddress'\]\['IpAddress'\]\[0\] print(AliCloudApi.hid, ele\['ZoneId'\], ele\['InstanceId'\], ele\['InstanceType'\], ele\['HostName'\], ele\['InstanceName'\], ele\['PublicIpAddress'\], ele\['InnerIpAddress'\]) AliCloudApi.hid += 1 # 每一個實例放入列表中 # print(ele) per\_region\_instances.append(ele) page\_num += 1 except Exception as e: print("ERROR: ", e) return per\_region\_instances def get\_all\_instances(self, region\_li): """ 得到全部region實例 :param region\_li: region列表 :return: """ for zone in region\_li: self.zone = zone print("\\033\[0;32mNow,start query %s region\\033\[0m" % zone) obj = AliCloudApi(self.key, self.secret, zone) per\_region\_instances = obj.queryinstances() AliCloudApi.all\_instances\_dic\[zone\] = per\_region\_instances return AliCloudApi.all\_instances\_dic if \_\_name\_\_ == '\_\_main\_\_': # 加載阿里雲訪問key key\_pair = yaml.load(open('aliyun.yml')) key = key\_pair\['key'\] secret = key\_pair\['secret'\] zones = \["cn-qingdao", "cn-hangzhou", "cn-beijing"\] # for zone in zones: # print("\\033\[1;32mNow,start query %s region\\033\[0m" % zone) # obj = AliCloudApi(key, secret, zone) # per\_region\_instances = obj.queryinstances() # all\_instances\_li\[zone\] = per\_region\_instances obj = AliCloudApi(key, secret) ret = obj.get\_all\_instances(zones)