这是老早写的,代码中用的资源均已未使用了,不涉及敏感信息,关键部份供参考
#!/usr/bin/env python
# coding: utf-8
# author: blog.ct99.cn
'''
功能介绍:
1、调用阿里云API,收集所有区域 ECS 信息
2、将需要的数据整理、生成 Excel 文档
3、关于阿里 sdk 的安装,api 的调用请参考阿里云官网
4、xlsxwriter 请参考这里:http://xlsxwriter.readthedocs.org/
'''
import json
from xlsxwriter import workbook
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import DescribeInstancesRequest
def get_sys_info(key, secret, zone):
'''
1、获取该区域全部主机详细信息
2、参数:cn-qingdao、cn-hangzhou、cn-beijing 等
'''
# 与阿里云建立有效连接
clt = client.AcsClient(key, secret, zone)
# 获取该区域全部实例详细信息
request = DescribeInstancesRequest.DescribeInstancesRequest()
# 将数据格式化成 json,默认为 XML
request.set_accept_format('json')
# 发起请求,获取数据
result = json.loads(clt.do_action(request)).get('Instances').get('Instance')
return result
def format_data(data_info):
'''
从全部数据中整理出需要的数据
'''
result = []
for line in data_info:
# print(line)
data = (
line.get('InstanceId'),
line.get('ZoneId'),
line.get('HostName'),
line.get('InstanceName'),
# line.get('PublicIpAddress').get('IpAddress'),
line["EipAddress"]["IpAddress"],
line.get('VpcAttributes').get('PrivateIpAddress').get("IpAddress"),
line.get('Cpu'),
line.get('Memory'),
line.get('EipAddress').get('Bandwidth'),
line.get('Status'),
line.get('CreationTime'),
line.get('ExpiredTime')
)
result.append(data)
return result
您暂时无权查看此隐藏内容!
def main():
key = 'xxxxxxxxxxxxx'
secret = 'xxxxxxxxxxxxx'
zones = ['cn-shenzhen', 'cn-hangzhou']
filename = './aliyunSystemToExcel.xlsx'
result = []
for zone in zones:
info = get_sys_info(key, secret, zone)
data = format_data(info)
[result.append(line) for line in data]
write_excel(filename, result)
if __name__ == '__main__':
main()
# str1 = """
测试用例略...
# """
# import json
# str1 = json.loads(str1)
# print(str1)
import requests
import time
import hashlib
import json
id = "xxxxxxxxxxxx"
password = "xxxxxxxxxxx"
timest = str(int(round(time.time() * 1000)))
m = hashlib.md5()
m.update(password.encode("utf-8"))
m.update(timest.encode("utf-8"))
sign = m.hexdigest()
def cye():
url = "http://139.129.128.71:8086/msgHttp/json/balance"
data = {"account": id, "password": sign, "timestamps": timest}
return json.loads(requests.post(url=url, data=data).text)["Count"]
def sendsms():
url = "http://sapi.appsms.cn:8088/msgHttp/json/mt"
mobile = "15000097030,18521313387"
m = hashlib.md5()
m.update(password.encode("utf-8"))
m.update(mobile.encode("utf-8"))
m.update(timest.encode("utf-8"))
sign1 = m.hexdigest()
content = "【epermarket】您的验证码:111111,有效时间30分钟。".encode("utf-8")
data = {"account": id, "password": sign1, "mobile": mobile, "content": content, "timestamps": timest}
return json.loads(requests.post(url=url, data=data).text)
print("短信剩余条数:", cye())
# print("短信发送结果:", sendsms())