Python伪AP扫描脚本:替换邮件通知为CSV存储需求
Got it, let's get this sorted. You want to ditch that clunky email alert system and save your fake AP detection results to a CSV file instead. Here's a straightforward, reliable way to do it, along with fixes for common issues that might have broken your custom script.
Step 1: Remove All Email-Related Code
First, delete the entire AlertAdmin function, plus any related imports (like import smtplib) and database queries that pull SMTP/admin email configs—you won't need any of that anymore.
Step 2: Add the CSV Logging Function
Add this function to handle writing detection results to a CSV. It includes error handling, auto-generates headers if the file is new, and appends results instead of overwriting them:
import csv import datetime def save_detection_to_csv(message): csv_path = "fake_ap_detections.csv" # Add a timestamp for better traceability (optional but recommended) current_time = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S") detection_data = [current_time, message] try: # Open file in append mode, use utf-8 to avoid encoding issues with open(csv_path, mode="a", newline="", encoding="utf-8") as csv_file: writer = csv.writer(csv_file) # Write header only if the file is empty csv_file.seek(0, 2) # Move cursor to end of file if csv_file.tell() == 0: writer.writerow(["Timestamp", "Fake AP Detection Message"]) # Write the detection entry writer.writerow(detection_data) print(f"✅ Saved detection result to {csv_path}") except Exception as e: print(f"❌ Failed to save to CSV: {str(e)}")
Step 3: Update Detection Calls
Find every place in your script where you called AlertAdmin(message) and replace it with save_detection_to_csv(message). That's all it takes to switch from emails to CSV logs.
Why Your Custom Script Might Have Failed
If your original attempt didn't work, it's likely one of these common issues:
- Overwriting instead of appending: You used
mode="w"instead ofmode="a", so each new detection erased the previous one. - Missing headers: You didn't check if the file was empty first, leading to missing headers or duplicate headers.
- Encoding problems: You didn't specify
encoding="utf-8", causing errors with special characters in your detection messages. - No error handling: You didn't wrap file operations in a try/except block, so you couldn't see why it failed.
Example Usage
Here's how it fits into your scanner workflow:
# When you detect a fake AP: detection_msg = f"Fake AP detected: SSID 'FreeWiFi' with MAC 00:11:22:33:44:55" save_detection_to_csv(detection_msg)
内容的提问来源于stack exchange,提问作者user3797683

