首页 » Python » python3 高级篇 » 正文

CMDB_基础Ali_api认知

获取accesskey

  • 登录阿里云;
  • 控制台;
  • 右上角头像 –> accesskeys;
  • 创建 accesskey (保存到文档,稍后用到);

基础Ali-api了解

API 概览(基础,磁盘,镜像,快照,安全)

例如 选择这个:

DescribeInstances	查询所有实例的详细信息。

如何使用呢? 如下 ↓

OpenApi Explorer

复制:“DescribeInstances” 到搜索框,出来二级选项框,选择一个区域就会出来咱们所对应不同语言的代码:

以Python为例:

#!/usr/bin/env python
#coding=utf-8

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-hangzhou')

request = CommonRequest()
request.set_accept_format('json')
request.set_domain('ecs.aliyuncs.com')
request.set_method('POST')
request.set_version('2014-05-26')
request.set_action_name('DescribeInstances')

request.add_query_param('RegionId', 'cn-hangzhou')

response = client.do_action(request)
# python2:  print(response) 
print(str(response, encoding = 'utf-8'))

然后修改刚刚保存的accesskey信息即可;

sdk 安装

python2
pip install aliyun-python-sdk-core

python3
pip install aliyun-python-sdk-core-v3

云产品 SDK 安装
pip install aliyun-python-sdk-ecs

运行:

{"PageNumber":1,"TotalCount":1,"PageSize":10,"RequestId":"BFE4F208-FB98-4156-A22D-3EE337A763B4","Instances":{"Instance":[{"ImageId":"centos_7_04_64_20G_alibase_201701015.vhd","VlanId":"","EipAddress":{"IpAddress":"","AllocationId":"","InternetChargeType":""},"ZoneId":"cn-hangzhou-b","IoOptimized":true,"SerialNumber":"bc1d494c-4cba-4e5b-b19f-eb6477f85f1b","Cpu":1,"Memory":2048,"DeviceAvailable":true,"SecurityGroupIds":{"SecurityGroupId":["sg-bp16sxv0bk2b9qcjisj3"]},"SaleCycle":"","AutoReleaseTime":"","OSType":"linux","ResourceGroupId":"","OSName":"CentOS  7.4 64","InstanceNetworkType":"vpc","HostName":"iZbp1iidpxlqigcvrjz7mjZ","CreationTime":"2018-04-01T12:41Z","RegionId":"cn-hangzhou","DeletionProtection":false,"OperationLocks":{"LockReason":[]},"ExpiredTime":"2021-04-01T16:00Z","InnerIpAddress":{"IpAddress":[]},"InstanceTypeFamily":"ecs.t5","InstanceId":"i-bp1iidpxlqigcvrjz7mj","NetworkInterfaces":{"NetworkInterface":[{"MacAddress":"00:16:3e:11:97:f4","PrimaryIpAddress":"172.16.79.206","NetworkInterfaceId":"eni-bp16sxv0bk2b9qcnb35o"}]},"InternetMaxBandwidthIn":200,"CreditSpecification":"Standard","InternetChargeType":"PayByBandwidth","SpotStrategy":"NoSpot","StoppedMode":"Not-applicable","InternetMaxBandwidthOut":1,"VpcAttributes":{"NatIpAddress":"","PrivateIpAddress":{"IpAddress":["172.16.79.206"]},"VSwitchId":"vsw-bp1jt078ogsy2q1ypz7ys","VpcId":"vpc-bp1tnyrlkqsyqhi5n3cdj"},"SpotPriceLimit":0.0,"StartTime":"2019-01-21T13:14Z","InstanceName":"zdh_zz","Description":"","PublicIpAddress":{"IpAddress":["47.98.156.84"]},"InstanceType":"ecs.t5-lc1m2.small","Status":"Running","Recyclable":false,"ClusterId":"","GPUSpec":"","GPUAmount":0,"InstanceChargeType":"PrePaid","DedicatedHostAttribute":{"DedicatedHostId":"","DedicatedHostName":""},"DeploymentSetId":""}]}}

信息比较多,可以放在json页面解析下;

当然一个最重要的信息是,阿里云区分不同的机器是根据实例id也就是:

"InstanceId": "i-bp1iidpxlqigcvrjz7mj"
赞 (0)

发表评论