Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import re
- import csv
- from datetime import datetime
- ROOT_DIR = r"C:\Users\Mayank\Documents\GitHub\ERP\hostel"
- # SQL keywords to scan
- SQL_KEYWORDS = [
- "SELECT", "INSERT", "UPDATE", "DELETE",
- "CREATE", "DROP", "ALTER", "TRUNCATE",
- "REPLACE", "GRANT", "REVOKE"
- ]
- # Compile regex (case-insensitive)
- pattern = re.compile(r"\b(" + "|".join(SQL_KEYWORDS) + r")\b", re.IGNORECASE)
- # Output file
- timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
- output_file = f"sql_keyword_scan_{timestamp}.csv"
- results = []
- # Walk through directory
- for root, _, files in os.walk(ROOT_DIR):
- for file in files:
- if file.lower().endswith(".php"):
- file_path = os.path.join(root, file)
- try:
- with open(file_path, "r", encoding="utf-8", errors="ignore") as f:
- for lineno, line in enumerate(f, start=1):
- matches = pattern.findall(line)
- if matches:
- for m in matches:
- results.append([
- file_path.strip(),
- lineno,
- m.upper(),
- line.strip()
- ])
- except Exception as e:
- print(f"Error reading {file_path}: {e}")
- # Write CSV
- with open(output_file, "w", newline="", encoding="utf-8") as csvfile:
- writer = csv.writer(csvfile)
- writer.writerow(["file_path", "line_number", "keyword", "line"])
- writer.writerows(results)
- print(f"[DONE] Output saved to {output_file}")
Advertisement