[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()