# generate_reports.py (fixed) import csv import subprocess import os from datetime import datetime REPORT_IDS_CSV = "data/report_ids.csv" CONSOLIDATED_CSV = "data/openvasscan.csv" TEMP_DIR = "temp_reports" def append_to_consolidated(temp_csv): if not os.path.exists(temp_csv): print(f"Warning: File {temp_csv} not found, skipping") return file_exists = os.path.isfile(CONSOLIDATED_CSV) with open(temp_csv, 'r') as infile, open(CONSOLIDATED_CSV, 'a', newline='') as outfile: reader = csv.reader(infile) writer = csv.writer(outfile) if file_exists: next(reader, None) for row in reader: writer.writerow(row) def main(): os.makedirs(TEMP_DIR, exist_ok=True) with open(REPORT_IDS_CSV, 'r') as f: reader = csv.DictReader(f) for row in reader: report_id = row['report_id'] timestamp = datetime.now().strftime('%Y%m%d_%H%M%S') base_name = f"report_{report_id}_{timestamp}" temp_csv = os.path.join(TEMP_DIR, base_name) cmd = [ 'gvm-script', '--gmp-username', 'admin', '--gmp-password', 'admin', # Fix typo here ("assword" → "password") 'tls', '--hostname', '127.0.0.1', '--port', '9390', 'export-csv-report.gmp.py', report_id, temp_csv ] try: result = subprocess.run( cmd, check=True, capture_output=True, text=True ) # Added closing parenthesis print(f"Command output: {result.stdout}") final_path = f"{temp_csv}.csv" if os.path.exists(final_path): append_to_consolidated(final_path) os.remove(final_path) print(f"Processed {final_path}") else: print(f"Error: Expected output file {final_path} not found") except subprocess.CalledProcessError as e: print(f"Error generating report {report_id}: {e.stderr}") print(f"Consolidation complete. Final file: {CONSOLIDATED_CSV}") if __name__ == "__main__": main() # Properly closed main function