Aucune description

starttask.py 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import csv
  2. from gvm.connections import TLSConnection
  3. from gvm.protocols.gmp import Gmp
  4. from gvm.transforms import EtreeTransform
  5. # OpenVAS Configuration
  6. OPENVAS_HOST = "localhost"
  7. OPENVAS_PORT = 9390
  8. USERNAME = "admin"
  9. PASSWORD = "admin"
  10. # CSV file
  11. TASK_ID_CSV = "task_id.csv"
  12. def start_task(gmp, task_id):
  13. """
  14. Start a task in OpenVAS.
  15. """
  16. try:
  17. gmp.start_task(task_id=task_id)
  18. print(f"Started task with ID: {task_id}")
  19. except Exception as e:
  20. print(f"Failed to start task with ID {task_id}. Error: {e}")
  21. def read_csv_to_task_list(csv_file):
  22. """
  23. Read the CSV file and extract task IDs into a list.
  24. """
  25. task_list = []
  26. with open(csv_file, newline="") as csvfile:
  27. reader = csv.DictReader(csvfile)
  28. for row in reader:
  29. task_list.append(row["Task ID"])
  30. return task_list
  31. def main():
  32. connection = TLSConnection(hostname=OPENVAS_HOST, port=OPENVAS_PORT)
  33. with Gmp(connection=connection, transform=EtreeTransform()) as gmp:
  34. # Authenticate with OpenVAS
  35. gmp.authenticate(username=USERNAME, password=PASSWORD)
  36. print("Authenticated with OpenVAS")
  37. # Read task IDs from the task_id.csv file
  38. task_list = read_csv_to_task_list(TASK_ID_CSV)
  39. if not task_list:
  40. print("No task IDs found in the CSV file. Exiting.")
  41. return
  42. print(f"Found {len(task_list)} tasks to start.")
  43. # Start each task
  44. for task_id in task_list:
  45. start_task(gmp, task_id)
  46. if __name__ == "__main__":
  47. main()