利用python生成F5 SSL 证书状态报表

1.背景

F5提供了各种配置和查询的restful API,通过这些API可以进行各种查询和配置。本文通过F5 提供的restful API 获取ssl 状态,并且筛选出即将过期的SSL 证书,发送邮件提醒。

2.思路

  • 使用F5 本身的API登陆 F5,获取token;
  • 利用token获取SSL 证书状态
  • 对SSL状态进行筛选
  • 发送邮件

3.F5官方文档参考

https://clouddocs.f5.com/api/icontrol-rest/

4.部分代码

4.1F5登陆,查询类

F5Operation 类中方法介绍:

  • login() :登陆F5,返回token
  • get_metric() :利用token和metric url ,获取相应的值,本次利用到的url是/mgmt/tm/sys/file/ssl-cert

本例中查询除了获取F5上SSL 证书状态外,还需要利用公司系统进行查询,不具有通用性,因此不在此贴出代码。

import requests
import json
import win32com

class F5Operation:
    def __init__(self, host, username, password):
        self.username = username
        self.password = password
        self.url = host 

    def login(self):
        auth_url = self.url + "/mgmt/shared/authn/login"
        data = {"username": self.username, "password": self.password, "loginProviderName": "tmos"}
        header = {"Content-Type": "application/json"}
        ret = requests.post(auth_url, data=json.dumps(data), verify=False, headers=header).json()
        if ret.get("token")
        	return ret['token']['token']
        else:
        	print("cannot get token of %s" %self.host)
        	sys.exit(1)

    def get_metric(self, metric_url):
        full_url = self.url + metric_url
        print(full_url)
        token = self.login()
        print(token)
        header = {"Content-Type": "application/json", "X-F5-Auth-Token": token}
        metric_ret = requests.get(full_url, headers=header, verify=False,).json()
        return metric_ret        

4.2邮件发送类

由于是在windows下编程,邮件发送可以选择win32com 中调用本机的outlook程序发送,也可以利用smtplib 发送邮件。因为smtplib需要设置smtp服务器地址,当前没有公司的smtp服务器地址,因此使用win32com 调用outlook发送邮件
win32com是功能强大的模块,可以调用window下各种程序。
邮件发送类实现如下:

import win32com.client as com
class OutLook:
    def __init__(self):
        pass
    @staticmethod
    def send_mail(revc, subject,body, html, display, cc=None,bcc=None):
        outlook = com.Dispatch("Outlook.Application")
        mail = outlook.CreateItem(0)
        mail.To = revc
        mail.Subject = subject
        if cc:
            mail.CC = cc
        if bcc:
            mail.BCC = bcc
        if html:
            """
            Set the format of mail
            1 - Plain Text
            2 - HTML
            3 - Rich Text
            """
            mail.BodyFormat = 2
        mail.body = body
        if display:
            mail.Display(True)
        else:
            mail.Send()

有了这两个类后,在主函数中即可实现需求。

5.遇到问题和解决过程

5.1使用url /mgmt/tm/sys/file/system-ssl-cert无法查询到证书状态

根据官方文档(https://clouddocs.f5.com/api/icontrol-rest/APIRef_tm_sys_file_system-ssl-cert.html)和web查看路径,初步认为/mgmt/tm/sys/file/system-ssl-cert 是获取ssl 证书状态的url
多次尝试,无法获取到证书状态,怀疑是权限或方法问题,尝试从/mgmt/tm/ltm/pool获取pool信息,获取成功,说明方法和权限没有问题;
退一步,使用/mgmt/tm/sys/file 获取file中的信息,获得一下信息:

{'kind': 'tm:sys:file:filecollectionstate', 'selfLink': 'https://localhost/mgmt/tm/sys/file?ver=12.1.4.1', 
'items': [{'reference': {'link': 'https://localhost/mgmt/tm/sys/file/apache-ssl-cert?ver=12.1.4.1'}},
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/browser-capabilities-db?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/dashboard-viewset?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/data-group?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/device-capabilities-db?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/external-monitor?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ifile?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/lwtunneltbl?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ssl-cert?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ssl-crl?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ssl-csr?ver=12.1.4.1'}}, 
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/ssl-key?ver=12.1.4.1'}},
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/system-ssl-cert?ver=12.1.4.1'}},
 {'reference': {'link': 'https://localhost/mgmt/tm/sys/file/system-ssl-key?ver=12.1.4.1'}}]}

尝试从/mgmt/tm/sys/file/ssl-cert获取,可以获取到ssl证书列表和状态信息。返回官方文档
https://clouddocs.f5.com/api/icontrol-rest/APIRef_tm_sys_file.html
可见文档中也提供了各种文件中的url。在使用url获取信息,且对该功能不熟悉时,不要盲目,先看官方文档,如果一个url无法获取,及时换另一个。

6.进一步思考

如果需要对多个F5 SSL 证书进行检查。可以利用利用多线进程方式实现。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值