新手来拜帖了,东拼西凑,网络交换机自动巡检并把巡检结果用邮件发送

查看 52|回复 7
作者:xzm6511085   
~~~~新手来拜帖了,东拼西凑,实现 网络交换机自动巡检并把巡检结果用邮件发送,本地也生成一份以日期为命名的TXT文档~~~~~
[Python] 纯文本查看 复制代码import paramiko
import datetime
import time
import smtplib
from email.mime.text import MIMEText
#f=open('./hostnames.txt','r') #准备好主机文件,一台主机占一行
#hostnames=f.readlines()
# 获取当前日期和时间
now = datetime.datetime.now()
# 将日期格式化为字符串,例如 "2023-10-05"
date_str = now.strftime("%Y-%m-%d")
# 创建文件名
filename = ("D:\\cron\\网络交换机日常监控\\" + f"{date_str}.txt")
import smtplib
from email.mime.text import MIMEText
from email.header import Header
# 设置服务器所需信息
mail_host = 'X.X.X.X'  # 邮件服务器地址
#mail_user = '[email protected]'  # 邮箱用户名
#mail_pass = 'your-password'  # 邮箱密码
# 邮件发送方邮箱地址
sender = '[email protected]'
# 邮件接收方邮箱地址
receivers = ['[email protected]']
hostnames=['XXX.XXX.XXX.XXXX' , 'XXX.XXX.XXX.XXXX'  , 'XXX.XXX.XXX.XXXX'  , 'XXX.XXX.XXX.XXXX'] #如果主机数量较少,可以以字符串的形式写入列表
for hostname in hostnames:
    hostname=hostname.strip()
    ssh_client=paramiko.SSHClient()
    ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh_client.connect(hostname=hostname,username='XXXXX',password='XXXXXXXXX') #连接登录网络设备
    print('Sucessfully conneted to ',hostname)
    cmd = ssh_client.invoke_shell()
    cmd.send('show version\n')
    time.sleep(1)
    result1 = cmd.recv(66666).decode('ascii')  # 将网络设备返回信息进行解码输出
    print(result1)
    with open(filename, "a") as file1:
        file1.write(result1)
    time.sleep(1)
    cmd.send('show logging buffer 1000 | include ALARM\n')
    time.sleep(1)
    result2 = cmd.recv(66666).decode('ascii')  # 将网络设备返回信息进行解码输出
    print(result2)
    time.sleep(1)
    with open(filename, "a") as file1:
        file1.write(result2)
    time.sleep(1)
    cmd.send('show logging buffer 1000 | include down\n')
    time.sleep(1)
    result3 = cmd.recv(66666).decode('ascii')  # 将网络设备返回信息进行解码输出
    print(result3)
    with open(filename, "a") as file1:
        file1.write(result3)
    time.sleep(1)
    cmd.send('show logging buffer 1000 | include errdisable\n')
    time.sleep(1)
    result4 = cmd.recv(66666).decode('ascii')  # 将网络设备返回信息进行解码输出
    print(result4)
    with open(filename, "a") as file1:
        file1.write(result4)
    time.sleep(1)
    cmd.send('show environment\n')
    time.sleep(1)
    result5 = cmd.recv(66666).decode('ascii')  # 将网络设备返回信息进行解码输出
    print(result5)
    with open(filename, "a") as file1:
        file1.write(result5)
    time.sleep(1)
    cmd.send('show processes cpu his\n')
    time.sleep(1)
    result6 = cmd.recv(66666).decode('ascii')  # 将网络设备返回信息进行解码输出
    print(result6)
    with open(filename, "a") as file1:
        file1.write(result6)
    time.sleep(1)
    cmd.send('show processes memory sorted | include Total\n')
    time.sleep(2)
    result7 = cmd.recv(66666).decode('ascii')  # 将网络设备返回信息进行解码输出
    print(result7)
    with open(filename, "a") as file1:
        file1.write(result7)
    time.sleep(2)
    # 设置email信息
    # 邮件内容设置
    message = MIMEText(result1 + result2 + result3 + result4 + result5 + result6 + result7, 'plain', 'utf-8')
    # 邮件主题
    message['Subject'] = Header('网络交换机日常巡检', 'utf-8')
    # 发送方信息
    message['From'] = sender
    # 接受方信息
    message['To'] = receivers[0]
    # 登录并发送邮件
    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("邮件发送成功")
    except smtplib.SMTPException as e:
        print("Error: 无法发送邮件", e)
    ssh_client.close()

网络设备, 信息

langjuxv   

一串代码把我劝退了。。。不过还是得佩服楼主一直在努力提升!
p100200300   

感谢分享
hushxh   

代码插入可以参考这个置顶帖
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/thread-713042-1-1.html
(出处: 吾爱破解论坛)
damer   

不明觉厉,顶你
xzm6511085
OP
  

一串代码把我劝退了。。。不过还是得佩服楼主一直在努力提升
xuu318   


苏紫方璇 发表于 2025-1-16 00:20
代码插入可以参考这个置顶帖
【公告】发帖代码插入以及添加链接教程(有福利)
https://www.52pojie.cn/t ...

谢谢版主知道~
xzm6511085
OP
  

斯国一,看着很好玩,感谢分享
您需要登录后才可以回帖 登录 | 立即注册

返回顶部