first commit
This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""测试脚本 - 运行10秒扫描并发送邮件"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
import time
|
||||
import datetime
|
||||
import subprocess
|
||||
|
||||
# 导入主脚本的配置和函数
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
import ping_monitor
|
||||
|
||||
def ping_host(ip, timeout=2):
|
||||
"""检测主机是否在线"""
|
||||
try:
|
||||
cmd = ["ping", "-c", "1", "-W", str(timeout), ip]
|
||||
result = subprocess.run(cmd, capture_output=True, text=True)
|
||||
return result.returncode == 0
|
||||
except Exception:
|
||||
return False
|
||||
|
||||
def main():
|
||||
print(f"[{datetime.datetime.now()}] 测试脚本启动")
|
||||
print(f"监控IP: {ping_monitor.TARGET_IPS}")
|
||||
print(f"天气位置: {ping_monitor.WEATHER_LOCATION}")
|
||||
print("-" * 50)
|
||||
|
||||
# 确保状态目录存在
|
||||
os.makedirs(ping_monitor.STATE_DIR, exist_ok=True)
|
||||
|
||||
# 获取天气和新闻
|
||||
print("正在获取天气和新闻...")
|
||||
weather_info = ping_monitor.get_weather()
|
||||
news_info = ping_monitor.get_hot_news()
|
||||
print(f"天气原始信息: {weather_info[:100]}...")
|
||||
print(f"新闻原始信息: {news_info[:100]}...")
|
||||
|
||||
weather_summary = ping_monitor.get_weather_summary(weather_info)
|
||||
news_summary = ping_monitor.get_news_summary(news_info)
|
||||
print(f"天气摘要: {weather_summary}")
|
||||
print(f"新闻摘要: {news_summary}")
|
||||
print("-" * 50)
|
||||
|
||||
# 测试Ping所有IP
|
||||
online_ips = []
|
||||
offline_ips = []
|
||||
for ip in ping_monitor.TARGET_IPS:
|
||||
online = ping_host(ip)
|
||||
if online:
|
||||
online_ips.append(ip)
|
||||
print(f"[{datetime.datetime.now()}] {ip} 在线")
|
||||
else:
|
||||
offline_ips.append(ip)
|
||||
print(f"[{datetime.datetime.now()}] {ip} 离线")
|
||||
|
||||
print("-" * 50)
|
||||
|
||||
# 加载状态
|
||||
state = ping_monitor.load_state()
|
||||
arrived_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
|
||||
# 如果有在线的IP,发送到岗邮件
|
||||
if online_ips:
|
||||
print(f"发送在线邮件 for: {online_ips}")
|
||||
subject, html = ping_monitor.build_email_html("arrived", arrived_time, weather_summary, news_summary)
|
||||
ping_monitor.send_email(subject, html)
|
||||
|
||||
# 如果有离线的IP,发送未到邮件
|
||||
if offline_ips:
|
||||
print(f"发送离线邮件 for: {offline_ips}")
|
||||
subject, html = ping_monitor.build_email_html("not_arrived", None, weather_summary, news_summary)
|
||||
ping_monitor.send_email(subject, html)
|
||||
|
||||
print("-" * 50)
|
||||
print(f"[{datetime.datetime.now()}] 测试完成")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user