首页 > 其他分享 >获取USG防火墙的NAT记录并发邮件给相应人员提醒

获取USG防火墙的NAT记录并发邮件给相应人员提醒

时间:2022-12-29 12:00:35浏览次数:42  
标签:USG 发邮件 ip list send NAT print inter port

授权USG NAT配置的时候需要遵循 NAT_张三_20221201_长期

import paramiko
import smtplib
from email.mime.text import MIMEText
from email.header import Header
import time


def get_usg_config():
    # 创造一个 SSH 客户端paramiko
    ssh_client = paramiko.SSHClient()  # 创建一个ssh客户端对象
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    ssh_client.connect(hostname='192.168.1.1', username='admin', password='123456')

    command = ssh_client.invoke_shell()

    command.send("display curr | include inside\n".encode())  # 发送dis cu 命令并编码

    time.sleep(5)  # 需要进行睡眠,以便让下一条接收响应的命令能够有时间回显
    recv = command.recv(65535).decode('gbk')  # recv接收响应,字节为65535,并进行解码

    result = recv.splitlines()[14:][:-1]
    return result
    ssh_client.close()


conf = get_usg_config()

# 获取发送邮件的内容
username2 = []
list_inter_ip = []
list_inter_port = []
list_lan_ip = []
list_lan_port = []
for i in conf:
    # print(i.split())
    try:
        if "nat" in i.split():
            # print(i.split())
            list = i.split()
            username1 = list[2].split("_")[1]
            username2.append(username1)
            # print(username)
            inter_ip = list[6]
            list_inter_ip.append(inter_ip)
            inter_port = list[7]
            list_inter_port.append(inter_port)
            lan_ip = list[9]
            list_lan_ip.append(lan_ip)
            lan_port = list[10]
            list_lan_port.append(lan_port)
    except:
        print("下标错误")
#print(username2)
#print(len(username2))
#print(list_inter_ip)
#print(len(list_inter_ip))
#print(list_inter_port)
#print(len(list_inter_port))
#print(list_lan_ip)
#print(len(list_lan_ip))
#print(list_lan_port)
#print(len(list_lan_port))

name_email_add = []
list_name_email = []
with open('email_addr.txt', encoding='utf-8') as file:
    content = file.read()
    name_list = content.rstrip().split()
for i in username2:
    # print(i)
    if i in name_list:
        count = name_list.index(i)
        name_email_add = name_list[count + 1]
        list_name_email.append(name_list[count + 1])
    else:
        name_email_add = ""
        list_name_email.append("")

num_send = 0
for i in username2:
    info_send = "使用人:" + username2[num_send] + "\n" + "外网IP:" + list_inter_ip[num_send] + "\n" + "外网端口:" + \
                list_inter_port[num_send] + "\n" + "内网IP:" + list_lan_ip[num_send] + "\n" + "内网端口:" + list_lan_port[
                    num_send]
    print(info_send)

    # 第三方 SMTP 服务
    mail_host = "smtp.163.com"  # 设置服务器
    mail_user = "xxx.com"  # 用户名
    mail_pass = "xxx"  # 口令

    sender = 'xxx.com'
    receivers = [list_name_email[num_send]]  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
    message = MIMEText(info_send, 'plain', 'utf-8')
    message['From'] = Header("防火墙外网映射提醒", 'utf-8')
    message['To'] = Header(list_name_email[num_send], 'utf-8')

    subject = '确认这条防火墙对外映射是否还在使用,如不使用请及时企业微信通知信息化停用'
    message['Subject'] = Header(subject, 'utf-8')

    try:
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host, 25)  # 25 为 SMTP 端口号
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print("邮件发送成功")
        num_send = num_send + 1
        time.sleep(10)
    except smtplib.SMTPException:
        print("Error: 无法发送邮件")
        num_send = num_send + 1

 

标签:USG,发邮件,ip,list,send,NAT,print,inter,port
From: https://www.cnblogs.com/wanglilu1987/p/17012135.html

相关文章