目录
一.前言
二.脚本中使用的库的介绍
2.1subprocess
2.2smtplib
2.3email
三.代码实现及解析
3.1导入必要的模块
3.2配置邮件参数
3.3定义发送邮件函数
3.4执行系统命令并检查输出
3.5主程序逻辑
四.致谢
在当今的数字化时代,服务器的稳定性和可靠性对于保障业务连续性至关重要。服务器监控作为预防性措施,能够帮助我们及时发现并解决潜在问题,避免服务中断。自动化监控系统不仅可以提高响应速度,还能减少人为疏漏,确保服务器环境的持续健康。
subprocess 模块用于在Python中执行外部命令,例如执行系统命令、调用其他可执行程序等。在运维脚本中,经常需要执行诸如操作系统命令、系统工具或者其他程序的任务。
smtplib 模块是Python的标准库之一,用于发送邮件。在运维脚本中,通常会用到这个模块来发送报警邮件、通知邮件等。
email 模块是Python标准库,用于处理电子邮件相关的数据结构和协议。在此脚本中,主要用来构建和处理邮件内容
import subprocess import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart import time # 设置邮件发送相关信息 smtp_server = 'smtp.example.com' smtp_port = 587 smtp_user = 'your_email@example.com' smtp_password = 'your_email_password' sender_email = 'your_email@example.com' receiver_email = 'recipient@example.com' def send_email(subject, body): msg = MIMEMultipart() msg['From'] = sender_email msg['To'] = receiver_email msg['Subject'] = subject msg.attach(MIMEText(body, 'plain')) # 登录SMTP服务器并发送邮件 with smtplib.SMTP(smtp_server, smtp_port) as server: server.starttls() server.login(smtp_user, smtp_password) server.send_message(msg) def check_server(hostname): result = subprocess.run(['ping', '-c', '1', hostname], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) if result.returncode == 0: return True else: return False if __name__ == "__main__": server_to_monitor = 'example.com' while True: if not check_server(server_to_monitor): print(f"Server {server_to_monitor} is down. Sending notification...") subject = f"Server {server_to_monitor} is down!" body = f"Server {server_to_monitor} is not responding to ping requests." send_email(subject, body) else: print(f"Server {server_to_monitor} is up.") time.sleep(300) # 每5分钟检查一次 import subprocess import smtplib from email.mime.text import MIMEText from email.mime.multipart import MIMEMultipart SMTP_SERVER = 'smtp.example.com' # SMTP服务器地址 SMTP_PORT = 587 # SMTP服务器端口号 SENDER_EMAIL = 'your_email@example.com' # 发件人邮箱地址 SENDER_PASSWORD = 'your_password' # 发件人邮箱密码 RECIPIENT_EMAIL = 'recipient@example.com' # 收件人邮箱地址 这些变量定义了SMTP服务器的地址和端口号,以及发件人和收件人的邮箱地址和凭据。
def send_email(subject, body): message = MIMEMultipart() message['From'] = SENDER_EMAIL message['To'] = RECIPIENT_EMAIL message['Subject'] = subject message.attach(MIMEText(body, 'plain')) try: with smtplib.SMTP(SMTP_SERVER, SMTP_PORT) as server: server.starttls() server.login(SENDER_EMAIL, SENDER_PASSWORD) server.send_message(message) print('Email sent successfully!') except Exception as e: print(f'Failed to send email. Error: {e}') send_email 函数通过SMTP连接发送邮件。它使用 MIMEMultipart 和 MIMEText 创建邮件对象,并附加主题、发件人、收件人以及邮件正文。smtplib.SMTP 连接到SMTP服务器,启用安全传输层(TLS),登录发件人邮箱,并使用 server.send_message 发送邮件。def execute_command(command): try: result = subprocess.run(command, capture_output=True, text=True) return result.stdout except Exception as e: return f'Error executing command: {e}' execute_command 函数接受一个命令字符串作为参数,使用 subprocess.run 执行该命令。capture_output=True 表示捕获命令的标准输出。text=True 表示命令的输出以文本形式返回。if __name__ == "__main__": command_to_execute = 'your_system_command_here' command_output = execute_command(command_to_execute) email_subject = 'Command Execution Report' email_body = f'The command "{command_to_execute}" executed successfully.\n\nOutput:\n{command_output}' send_email(email_subject, email_body) if __name__ == "__main__": 块中,定义要执行的系统命令字符串,并调用 execute_command 函数获取命令执行结果。send_email 函数发送包含执行结果的邮件。非常感谢您阅读我的博客!如果您有任何问题、建议或想了解特定主题,请随时告诉我。您的反馈对我非常重要,我将继续努力提供高质量的内容。
如果您喜欢我的博客,请考虑订阅我们的更新,这样您就不会错过任何新的文章和信息。同时,欢迎您分享我们的博客给更多的朋友和同事,让更多人受益。
再次感谢您的支持和关注!如果您有任何想法或需求,请随时与我们联系。祝您生活愉快,学习进步!