阿里云获取所有实例信息 API 使用
阿里云获取所有实例信息 API 使用
使用前需安装依赖的sdk模块,可以在阿里云后台查询相关API使用方式:
示例代码:
#! /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)