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

CMDB_阿里Api详解

一、接上节课优化:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019-1-26 11:53
# @Author  : zhdya@zhdya.cn
# @File    : Ali_api.py

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import json

client = AcsClient('accesskey-id', 'accesskey', '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'))       ##打印出的内容是字符串
ret = json.loads(response.decode('utf-8'))        ##这样打印出的内容就是字典(这样我们就可以随便取值了)
print(ret)

print(ret.get('RequestId'))

查看下:

print(ret.get('RequestId'))

595D5730-C5CC-4C06-86E7-CE90269BF6C2

筛选需要的值

先来看下如上ret的值经过json处理后的结果:(部分数据)

mark

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import json

client = AcsClient('XXXXXX', 'XXXXXXXXXXXXXX', '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'))
ret = json.loads(response.decode('utf-8'))

ret_data = ret.get('Instances').get('Instance')
for item in ret_data:
	print('区域:', item.get('ZoneId'))
	print('内存:', item.get('Memory'))
	print('Cpu', item.get('Cpu'))
	print('OS类型:', item.get('OSName'))
	print('创建日期:', item.get('CreationTime'))
	print('过期日期:', item.get('ExpiredTime'))
	print('唯一标识:', item.get('InstanceId'))
	net_info = item.get('PublicIpAddress')
	print('公网IP', net_info.get('IpAddress'))

输出:

区域: cn-hangzhou-b
内存: 2048
Cpu 1
OS类型: CentOS  7.4 64
创建日期: 2018-04-01T12:41Z
过期日期: 2021-04-01T16:00Z
唯一标识: i-bp1iidpxlqigcvrjz7mj
公网IP ['47.98.156.84']

当然CMDB需要的是一组字典数据,我们需要重新封装下:

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import json

client = AcsClient('XXXXXXX', 'XXXXXXXXXXXX', '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'))
ret = json.loads(response.decode('utf-8'))

result_dic = {}
ret_data = ret.get('Instances').get('Instance')
for item in ret_data:
	net_info = item.get('PublicIpAddress')
	result_dic[item.get('InstanceId')] = {'InstanceId':item.get('InstanceId')}
	result_dic[item.get('InstanceId')]['Cpu'] = item.get('Cpu')
	result_dic[item.get('InstanceId')]['OS'] = item.get('OSName')
	result_dic[item.get('InstanceId')]['Mem'] = item.get('Memory')
	result_dic[item.get('InstanceId')]['ZoneId'] = item.get('ZoneId')
	result_dic[item.get('InstanceId')]['CreateTime'] = item.get('CreationTime')
	result_dic[item.get('InstanceId')]['EndTime'] = item.get('ExpiredTime')
	result_dic[item.get('InstanceId')]['PublicIp'] = net_info.get('IpAddress')
	print(result_dic)

输出看下最终的结果:

{'i-bp1iidpxlqigcvrjz7mj': {'InstanceId': 'i-bp1iidpxlqigcvrjz7mj', 'EndTime': '2021-04-01T16:00Z', 'OS': 'CentOS  7.4 64', 'PublicIp': ['47.98.156.84'], 'CreateTime': '2018-04-01T12:41Z', 'Mem': 2048, 'ZoneId': 'cn-hangzhou-b', 'Cpu': 1}}

关于磁盘信息

阿里云 API 中磁盘信息通过单独的接口调用,在云主机中没有磁盘信息;

通过这个接口:

DescribeDisks

然后在 OpenApi Explorer 平台 中查询代码即可!

#!/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('DescribeDisks')

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

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

和如上一个步骤,我们就可以获取到磁盘的详细信息:

点我查看:具体参数信息

当然阿里封装的API有很多,也包括常用的 关闭,重启,启动一台ecs等;

还有一个作为运维特别使用的 webssh 阿里也帮我们封装ok了;

调用该接口时,您需要注意:

  • 管理终端地址的有效期为 15 秒,调用接口成功后如果 15 秒内不使用该链接,URL 地址自动失效,您需要重新查询。
  • 单个管理终端链接的持久链接(KeepAlive)时间为 60 秒,60 秒内您管理终端窗口没有任何交互操作时,连接自动断开。
  • 如果连接中断,每分钟内重新连接的次数不能超过 30 次。

使用方法

您需要在如下链接:

https://g.alicdn.com/aliyun/ecs-console-vnc/0.0.7/index.html? 末尾加上 vncUrl=xxxx instanceId=xxx  isWindows=true isWindows=false  password=XXXXXX,参数之间使用 & 连接。其中参数 vncUrl:调用接口成功后会返回的 VncUrl 的值。
  • 参数 instanceId:您的实例 ID。
  • 参数 isWindows:该实例的操作系统是否是 Windows 系统。取值为 true 表示是 Windows 系统,取值为 false 表示不是 Windows 系统。
  • (可选)参数 password:该实例的远程连接密码,由 6 位数字或大小写字母组成。使用该参数时,在连接管理终端处您不需要再输入密码。

示例:

https://g.alicdn.com/aliyun/ecs-console-vnc/0.0.7/index.html?vncUrl=ws%3A%2F%xxx&instanceId=i-wz9hhwq5a6tmxxxxxxx&isWindows=true

或:

https://g.alicdn.com/aliyun/ecs-console-vnc/0.0.7/index.html?vncUrl=ws%3A%2F%xxx&instanceId=i-wz9hhwq5a6tmxxxxxxx&isWindows=true&Password=111111

代码如下:

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

from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
import json

client = AcsClient('XXXX', 'XXXXXXXXXXX', '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('DescribeInstanceVncUrl')

request.add_query_param('RegionId', 'cn-hangzhou')
request.add_query_param('InstanceId', 'i-bp1iXXXXXXXXXmj')

response = client.do_action(request)
# python2:  print(response)
# print(str(response, encoding = 'utf-8'))
ret = json.loads(response.decode('utf-8'))
vncurl = 'vncUrl=' + ret.get('VncUrl')
# print(vncurl)
basicUrl = "https://g.alicdn.com/aliyun/ecs-console-vnc/0.0.7/index.html?"
instanceId = 'instanceId=i-bp1iXXXXXXXXXmj'
isWindows = 'isWindows=false'
a = '&'
print(basicUrl+vncurl+a+instanceId+a+isWindows)

打印结果为:

https://g.alicdn.com/aliyun/ecs-console-vnc/0.0.7/index.html?vncUrl=wss%3A%XXXXXXFhz01-vncproxy.aliyun.com%2Fwebsockify%2F%3Fs%3DU5ddnEFHagDx0mRYtnYbpXXXXXXXgWv9VNICEQ7CPszGI%252Br7vMTHExgu7EiFz9t&instanceId=i-bp1iXXXXXXXXXmj&isWindows=false

访问结果如下:

mark

赞 (1)

发表评论