使用python 实现
文件名 >> get_dns_ecs_list.py
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkdomain.request.v20180129.QueryDomainListRequest import QueryDomainListRequest #dns
from aliyunsdkecs.request.v20140526.DescribeInstancesRequest import DescribeInstancesRequest #ecd
from aliyunsdkecs.request.v20140526.DescribeRegionsRequest import DescribeRegionsRequest #locaction
from dingtalkchatbot.chatbot import DingtalkChatbot
import datetime, json, time
#跳过需要检测的域名
fs = open("./skip_doname", "r")
skip_doname = fs.read()
skip_doname = skip_doname.split('\n')
fs.close()
#dns分页大小
page_size = 1
#获取一个30天内的日期
timelist = []
for i in range(0, 90):
timelist.append((datetime.datetime.now() + datetime.timedelta(days=i)).strftime("%Y-%m-%d"))
class get_locaction:
def __init__(self, accessKeyId, accessSecret):
self.client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')
def get_info(self):
request = DescribeRegionsRequest()
request.set_accept_format('json')
response = self.client.do_action_with_exception(request)
locaction_list = []
for i in json.loads(str(response, encoding='utf-8'))['Regions']['Region']:
locaction_list.append(i['RegionId'])
return locaction_list
class get_dns:
def __init__(self, accessKeyId, accessSecret):
self.page_size = 50
self.client = AcsClient(accessKeyId, accessSecret, 'cn-hangzhou')
#获取页面中的域名 load 传入的时页面
def get_page_reading(self,load):
request = QueryDomainListRequest()
request.set_accept_format('json')
request.set_PageNum(int(load))
request.set_PageSize(int(self.page_size))
response = self.client.do_action_with_exception(request)
dns_list = []
for i in json.loads(str(response, encoding='utf-8'))['Data']['Domain']:
dns_list.append({'doname': i['DomainName'], 'end_time': i['ExpirationDate'].split()[0]})
return dns_list
#获取总页数
def Total_page_num(self):
request = QueryDomainListRequest()
request.set_accept_format('json')
request.set_PageNum(1)
request.set_PageSize(int(self.page_size))
response = self.client.do_action_with_exception(request)
#分页数
return int(json.loads(str(response, encoding='utf-8'))['TotalPageNum'])
#翻页获取信息
def get_info(self):
dns_list = []
for i in range(0,self.Total_page_num()):
i=i+1
dns_list = dns_list + self.get_page_reading(i)
return dns_list
class get_ecs:
def __init__(self, accessKeyId, accessSecret):
self.accessKeyId = accessKeyId
self.accessSecret = accessSecret
#获取列表
def get_host_all(self, zone):
self.client = AcsClient(self.accessKeyId, self.accessSecret, zone)
request = DescribeInstancesRequest()
request.set_accept_format('json')
response = self.client.do_action_with_exception(request)
ecs_list = []
for i in json.loads(str(response, encoding='utf-8'))["Instances"]['Instance']:
ecs_list.append({"id": i['InstanceId'],
"name": i['InstanceName'],
"end_time": i['ExpiredTime'].split('T')[0],
"ipaddr": i['PublicIpAddress']['IpAddress'][0]})
return ecs_list
#获取所有主机
def get_info(self):
all_locaction = get_locaction(self.accessKeyId, self.accessSecret)
ecs_list = []
for i in all_locaction.get_info():
for i in self.get_host_all(i):
ecs_list.append(i)
return ecs_list
class check:
def __init__(self,security):
self.security = security
def __check_ecs_dns(self,security):
info_ecs_list = []
info_dns_list = []
fs.close()
#处理ecs获取即将到期列表
x = get_ecs(security['accessKeyId'], security['accessSecret'])
for i in x.get_info():
if i['end_time'] in timelist:
try:
i.update(aliyun=security['aliyun_name'])
info_ecs_list.append(i)
except Exception:
continue
#处理dns获取即将到期列表
x = get_dns(security['accessKeyId'], security['accessSecret'])
for i in x.get_info():
if i['end_time'] in timelist:
if i['doname'] in skip_doname:
print("skip_donamt: " + i['doname'])
else:
try:
i.update(aliyun=security['aliyun_name'])
info_dns_list.append(i)
except Exception:
continue
return info_dns_list + info_ecs_list
def run(self):
for i in self.security:
for i in self.__check_ecs_dns(i):
if len(i) == 3:
#print("域名:" + str(i))
android_user.send_text("在30天内到期的域名!!!!\n阿里云账号:" + i['aliyun'] + "\n到期的域名:" + i['doname'] + "\n到期时间" + i['end_time'] )
#print("在30天内到期的域名!!!!\n阿里云账号:" + i['aliyun'] + "\n到期的域名:" + i['doname'] + "\n到期时间" + i['end_time'])
elif len(i) == 5:
android_user.send_text("在30天内到期的服务器!!!!\n阿里云账号:" + i['aliyun'] +" \n服务器名称:" + i['name'] + "\n服务器id:" + i['id'] + "\n到期时间:" + i['end_time'])
#print("在30天内到期的服务器!!!!\n阿里云账号:" + i['aliyun'] + " \n服务器名称:" + i['name'] + "\n服务器id:" + i['id'] + "\n到期时间:" + i['end_time'])
#print("服务器名称:" + str(i))
security = '使用exec函数运行keyfile中的代码,security返回阿里云授权列表何以及账号信息'
android_user = '使用exec函数运行keyfile中的代码,android_user 反回钉钉对象'
fs = open("./keyfile", "r", encoding='UTF-8')
exec(fs.read())
fs.close()
x = check(security)
x.run()
文件名 >> keyfile
#需要阿里云给的权限
#管理域名服务的权限
#只读访问云服务器服务(ECS)的权限
#根据来运提供的
security = [{'accessKeyId': '
#钉钉机器人地址写入send_android_url中即可实现
send_android_url = ""
android_user = DingtalkChatbot(send_android_url)
文件名 >> skip_doname
可以时空文件,每一行可以写入一个域名,写入该文件的域名将被跳过检测