python获取aliyun ECS实例
#!/usr/bin/env python
#-*- coding:utf-8 -*-
# Description : get ecs from aliyun
# Author : quke
# Date : 2017-05-16 import json
import math
import time
import random
import datetime
import sqlite3
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest def coroutine(func):
def start(*args, **kwargs):
g = func(*args, **kwargs)
g.next()
return g
return start @coroutine
def get_aliyun_ecs(key, secret, zone):
'''
aliyun显示每页最大显示数量PageSize 100条
https://help.aliyun.com/document_detail/35112.html
'''
Client = client.AcsClient(key, secret, zone) request = DescribeInstancesRequest.DescribeInstancesRequest()
request.set_accept_format('json')
request.set_PageSize(100)
response = None while True:
pageNumber = (yield response)
print pageNumber
request = DescribeInstancesRequest.DescribeInstancesRequest()
request.set_accept_format('json')
request.set_PageSize(100)
request.set_PageNumber(pageNumber)
response = json.loads(Client.do_action_with_exception(request)) def format_data(jsonData):
instances = jsonData.get('Instances').get('Instance')
result = [] for line in instances:
data = (
line.get('ZoneId'),
line.get('InstanceId'),
line.get('HostName'),
line.get('InnerIpAddress').get('IpAddress')[0] if line.get('InnerIpAddress').get('IpAddress') else 'null',
line.get('PublicIpAddress').get('IpAddress')[0] if line.get('PublicIpAddress').get('IpAddress') else 'null',
u'%d核|%dG' %(line.get('Cpu'),int(line.get('Memory'))/1024),
datetime.datetime.strptime(line.get('CreationTime'), '%Y-%m-%dT%H:%MZ').strftime('%Y-%m-%d %H:%M:%S'),
datetime.datetime.strptime(line.get('ExpiredTime'), '%Y-%m-%dT%H:%MZ').strftime('%Y-%m-%d %H:%M:%S'),
)
result.append(data) return result def insertDB(data):
conn = sqlite3.connect('/Users/quke/db.sqlite3')
cursor = conn.cursor() for line in data:
cursor.execute('insert into cmdb_aliyunecs("region","instanceId","hostname","inner_ip","outer_ip","hardware","creationTime","expiredTime") values(?,?,?,?,?,?,?,?)', line)
conn.commit() if __name__ == '__main__':
# region https://help.aliyun.com/document_detail/28908.html
regions = ('cn-beijing', 'cn-hangzhou', 'cn-hongkong', 'us-east-1')
for region in regions:
print region
aliyun_ecs=get_aliyun_ecs('key','secret', region)
jsonData = aliyun_ecs.send(1)
formated_data = format_data(jsonData)
insertDB(formated_data) totalCount = jsonData.get('TotalCount')
totalPage = int(math.ceil(totalCount/float(100))) + 1 for pageNumber in range(2, totalPage):
time.sleep(random.random())
jsonData = aliyun_ecs.send(pageNumber)
formated_data = format_data(jsonData)
insertDB(formated_data)
python获取aliyun ECS实例的更多相关文章
- 【类不类二】Python的类变量与实例变量
在研究类的时候,难免会有很多疑问,C论坛和博客园高手如云(不知道是不是也美女如云), 搜到了这篇博文,是介绍Python的类变量和实例变量的 ! 刚好在下对self.***这种形式的实例变 量不是很理 ...
- shell,python获取当前路径(脚本的当前路径) (aso项目记录)
一.shell获取脚本当前路径 cur_dir=$(cd "$(dirname "$0")"; pwd) #获取当前脚本的绝对路径,参数$0是当前脚本对象 等 ...
- python获取日期加减之后的日期
python语言中的datetime模块可以利用其中的方法获取不同的日期,比如获取当前日期.明天.昨天.上个月.下个月和明年.下面利用几个实例说明这些日期的获取方法,操作如下: 第一步,利用d ...
- 【转】基于Python的接口测试框架实例
下面小编就为大家带来一篇基于Python的接口测试框架实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考.一起跟随小编过来看看吧 背景 最近公司在做消息推送,那么自然就会产生很多接口,测试 ...
- 阿里云自定义镜像可以免费保存,ECS实例到期后自定义镜像手动快照不会被删除
阿里云自定义镜像可以免费保存,ECS实例到期后自定义镜像手动快照不会被删除 4. ECS 实例释放后,自定义镜像是否还存在? 存在. 5. ECS 实例释放后,快照是否还存在? 保留手动快照,清除自动 ...
- redis安装 phpredis Jedis 扩展的实现及注意事项,php,java,python相关插件安装实例代码和文档推荐
redis安装 phpredis Jedis 扩展的实现及注意事项,php,java,python相关插件安装实例代码和文档推荐 1.Redis 官方网站下载: http://redis.io/dow ...
- 基于Python的接口测试框架实例
文章来源:http://www.jb51.net/article/96481.htm 下面小编就为大家带来一篇基于Python的接口测试框架实例.小编觉得挺不错的,现在就分享给大家,也给大家做个参考. ...
- Python的装饰器实例用法小结
这篇文章主要介绍了Python装饰器用法,结合实例形式总结分析了Python常用装饰器的概念.功能.使用方法及相关注意事项 一.装饰器是什么 python的装饰器本质上是一个Python函数,它可以让 ...
- Python获取百度浏览记录
Python模拟百度登录实例详解 http://www.jb51.net/article/78406.htm Python实战计划学习作业2-1 http://blog.csdn.net/python ...
随机推荐
- 迷你MVVM框架 avalonjs 0.93发布
这段时间吸取@limodou, @东灵等人的意见,做了以下改进 重构isArrayLike,提高avalon.each的性能,原来avalon.each是依赖于isArrayLike来判定是循环普通对 ...
- thymeleaf 获取项目路径
<p th:text=${salecode}></p> <a th:href="${#httpServletRequest.getScheme()+'://'+ ...
- linux 关闭电子邮件传输服务
1.检查sendmail的哪些runlevel开启: chkconfig --list sendmail
- 安装bcmath 扩展
1.在php源码包中,默认就包含bcmath扩展的安装文件,只需手动安装一下即可 cd /root/build2/php-/ext/bcmath // 进入PHP的源码包目录中的bcmatch扩展目录 ...
- web02
高内聚,低耦合 写what 不写how 我们只关心他是什么,得到什么,我们并不关心怎么去得到的 ,那个细节去怎么得的, 都应该在这个层面上屏蔽掉,要关心的时候在点进去,这样就一层层的结构良好的代码 d ...
- ORACLE DBLINK 使用
CREATE PUBLIC DATABASE LINK MYDBLINK CONNECT TO RAMS IDENTIFIED BY RAMS USING '(DESCRIPTION =(ADDRES ...
- 441. Arranging Coins
static int wing=[]() { std::ios::sync_with_stdio(false); cin.tie(NULL); ; }(); class Solution { publ ...
- 各大主流.Net的IOC框架性能测试比较(转)
出处:http://www.cnblogs.com/liping13599168/archive/2011/07/17/2108734.html 在上一篇中,我简单介绍了下Autofac的使用,有人希 ...
- js 中的 2 与 "2"
case1: "15" * 2 结果:30 case2: 2 * "15" 结果:30 case3: "2" * "15" ...
- alertView 上添加textField
- (void)showTextFieldUIAlertView { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@ ...