Lightweight Vulnerability Scanner for Resourced-constrained Organizations

generate_reports copy.py 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # generate_reports.py (fixed)
  2. import csv
  3. import subprocess
  4. import os
  5. from datetime import datetime
  6. REPORT_IDS_CSV = "data/report_ids.csv"
  7. CONSOLIDATED_CSV = "data/openvasscan.csv"
  8. TEMP_DIR = "temp_reports"
  9. def append_to_consolidated(temp_csv):
  10. if not os.path.exists(temp_csv):
  11. print(f"Warning: File {temp_csv} not found, skipping")
  12. return
  13. file_exists = os.path.isfile(CONSOLIDATED_CSV)
  14. with open(temp_csv, 'r') as infile, open(CONSOLIDATED_CSV, 'a', newline='') as outfile:
  15. reader = csv.reader(infile)
  16. writer = csv.writer(outfile)
  17. if file_exists:
  18. next(reader, None)
  19. for row in reader:
  20. writer.writerow(row)
  21. def main():
  22. os.makedirs(TEMP_DIR, exist_ok=True)
  23. with open(REPORT_IDS_CSV, 'r') as f:
  24. reader = csv.DictReader(f)
  25. for row in reader:
  26. report_id = row['report_id']
  27. timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
  28. base_name = f"report_{report_id}_{timestamp}"
  29. temp_csv = os.path.join(TEMP_DIR, base_name)
  30. cmd = [
  31. 'gvm-script',
  32. '--gmp-username', 'admin',
  33. '--gmp-password', 'admin', # Fix typo here ("assword" → "password")
  34. 'tls', '--hostname', '127.0.0.1', '--port', '9390',
  35. 'export-csv-report.gmp.py',
  36. report_id,
  37. temp_csv
  38. ]
  39. try:
  40. result = subprocess.run(
  41. cmd,
  42. check=True,
  43. capture_output=True,
  44. text=True
  45. ) # Added closing parenthesis
  46. print(f"Command output: {result.stdout}")
  47. final_path = f"{temp_csv}.csv"
  48. if os.path.exists(final_path):
  49. append_to_consolidated(final_path)
  50. os.remove(final_path)
  51. print(f"Processed {final_path}")
  52. else:
  53. print(f"Error: Expected output file {final_path} not found")
  54. except subprocess.CalledProcessError as e:
  55. print(f"Error generating report {report_id}: {e.stderr}")
  56. print(f"Consolidation complete. Final file: {CONSOLIDATED_CSV}")
  57. if __name__ == "__main__":
  58. main() # Properly closed main function