Python boto3 遍历credential获取aws EC2基本信息
2021/10/10 20:14:01
本文主要是介绍Python boto3 遍历credential获取aws EC2基本信息,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
获取凭证的AKSK
import csv import os class ReadCredentials: def __init__(self): self.file_dir = 'D:/BaiduNetdiskWorkspace/代码/python/aws/Credentials' def file_name(self): """ :return: 返回credentials下所有的文件名 """ return os.listdir(self.file_dir) def read_csv(self, filename): """ :param filename: Credential 的文件名 :return: 将 Credential 里面的内容以 json 的格式返回 """ tableData = [] filename = self.file_dir + '/' + filename with open(filename, 'r', encoding='utf-8') as csvfile: reader = csv.DictReader(csvfile) for row in reader: tableData.append(dict(row)) return tableData def get_credential(self): """ :return: 把所有 credential 整合输出 """ file_names = self.file_name() credentials = [] for file_name in file_names: credentials.append(self.read_csv(file_name)[0]) return credentials if __name__ == "__main__": print(ReadCredentials().get_credential())
获取EC2的信息
# -*- coding: utf-8 -*- """ @Time : 2021/10/9 10:07 @Auth : ndmiao @Blog :www.ndmiao.cn @Url :https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ec2.html#EC2.Client.describe_instances """ import openpyxl import boto3 from ReadCredentials import ReadCredentials from openpyxl.styles import Alignment class GetEC2Information: def __init__(self): self.region = 'cn-north-1' def ec2_client(self, id, key, region): """ :param id: aws_access_key_id :param key: aws_secret_access_key :param region: region_name :return: 建立一个与 ec2 的连接 """ ec2 = boto3.client( 'ec2', aws_access_key_id = id, aws_secret_access_key = key, region_name = region ) return ec2 def ec2_information(self): """ :return: 获取所有账号下ec2资源信息,保存到xlsx里面 """ credentials = ReadCredentials().get_credential() wb = WriteToXlsx().active_xlsx() for credential in credentials: ec2 = self.ec2_client(credential['Access key ID'], credential['Secret access key'], self.region) instances = ec2.describe_instances() instance_info = [] for item in instances['Reservations']: owner_id = item['OwnerId'] for instance in item['Instances']: InstanceId = instance['InstanceId'] InstanceType = instance['InstanceType'] PrivateIpAddress = instance['PrivateIpAddress'] State = instance['State']['Name'] Tags = instance['Tags'] Name = self.get_tag(Tags, 'Name') Project = self.get_tag(Tags, 'project') Schedule = self.get_tag(Tags, 'Schedule') ScheduleMessage = self.get_tag(Tags, 'ScheduleMessage') instance_info.append([Name, InstanceId, InstanceType, PrivateIpAddress, Project, State, Schedule, ScheduleMessage]) WriteToXlsx().send_data(wb, owner_id, instance_info) WriteToXlsx().save(wb) def get_tag(self, Tags, key): value = [tag['Value'] for tag in Tags if tag['Key'] == key] try: value = value[0] except: value = 'None' return value class WriteToXlsx: def __init__(self): self.headers = ['Name', 'InstanceId', 'InstanceType', 'PrivateIpAddress', 'Project', 'State', 'Schedule', 'ScheduleMessage'] self.xlsx_name = 'D:/BaiduNetdiskWorkspace/代码/python/aws/EC2/ResoursesList.xlsx' def active_xlsx(self): """ :return: 创建一个 xlsx 连接 """ wb = openpyxl.Workbook() return wb def send_data(self, wb, sheet_name, sheet_data): """ :param wb: wb :param sheet_name: 表名 :param sheet_data: 写入的数据 :return: 生成一个 sheet 并写入数据 """ ws = wb.create_sheet(sheet_name) ws.append(self.headers) for data in sheet_data: ws.append(data) self.center(wb, sheet_name) def center(self, wb, sheet_name): table = wb[sheet_name] rows = table.max_row cols = table.max_column alignobj = Alignment(horizontal='center', vertical='center', wrap_text=True) for r in range(rows + 1): for c in range(cols + 1): if r != 0 and c != 0: table.cell(row=r, column=c).alignment = alignobj def save(self, wb): """ :return: 保存 """ wb.remove(wb['Sheet']) wb.save(self.xlsx_name) if __name__ == "__main__": GetEC2Information().ec2_information()
GitHub @ndmiao ·
BiliBili @南岛鹋 ·
知乎 @南岛鹋 ·
CSDN @南岛鹋 ·
博客园 @南岛鹋 ·
个人站点 @南岛鹋
打赏
支付宝 | 微信 |
---|---|
这篇关于Python boto3 遍历credential获取aws EC2基本信息的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门