forked from greenbone/gvm-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
start-scans-from-csv.py
executable file
·157 lines (133 loc) · 4.61 KB
/
start-scans-from-csv.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# -*- coding: utf-8 -*-
#
# Loosely based on Greenbone sample scripts
# As provided by Greenbone in the gvm-tools repo
#
# Martin Boller
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Run with gvm-script --gmp-username admin-user --gmp-password password socket start-scans-from-csv.gmp.py startscans.csv
#
import sys
import time
import csv
import json
from argparse import ArgumentParser, Namespace, RawTextHelpFormatter
from pathlib import Path
from typing import List
from gvm.errors import GvmResponseError
from gvm.protocols.gmp import Gmp
from gvmtools.helper import error_and_exit
HELP_TEXT = (
"This script pulls task names from a csv file and starts the tasks listed in every row. \n"
)
def check_args(args):
len_args = len(args.script) - 1
if len_args != 2:
message = """
This script pulls tasks from a csv file and creates a \
task for each row in the csv file.
One parameter after the script name is required.
1. <tasks_csvfile> -- csv file containing names and secrets required for scan tasks
Example:
$ gvm-script --gmp-username name --gmp-password pass \
ssh --hostname <gsm> scripts/start_tasks_from_csv.gmp.py \
<tasks-csvfile>
"""
print(message)
sys.exit()
def parse_args(args: Namespace) -> Namespace: # pylint: disable=unused-argument
"""Parsing args ..."""
parser = ArgumentParser(
prefix_chars="+",
add_help=False,
formatter_class=RawTextHelpFormatter,
description=HELP_TEXT,
)
parser.add_argument(
"+h",
"++help",
action="help",
help="Show this help message and exit.",
)
parser.add_argument(
"task_file",
type=str,
help=("CSV File containing tasks"),
)
script_args, _ = parser.parse_known_args(args)
return script_args
def task_id(
gmp: Gmp,
task_name: str,
):
response_xml = gmp.get_tasks(filter_string="rows=-1, not status=Running and "
"not status=Requested and not "
"status=Queued "
"and name=" + task_name)
tasks_xml = response_xml.xpath("task")
task_id = ""
for task in tasks_xml:
name = "".join(task.xpath("name/text()"))
task_id = task.get("id")
#print("Requesting start of task: " + task_name + " Task UUID: " + task_id)
return task_id
def start_tasks(
gmp: Gmp,
task_file: Path,
):
try:
numbertasks = 0
with open(task_file, encoding="utf-8") as csvFile:
content = csv.reader(csvFile, delimiter=',') #read the data
try:
for row in content: #loop through each row
if len(row) == 0:
continue
task_start = task_id(gmp, row[0])
if task_start:
numbertasks = numbertasks + 1
print(f"Starting task name: {row[0]} with uuid: {task_start} ...")
status_text = gmp.start_task(task_start).xpath(
"@status_text"
)[0]
print(status_text)
else:
print("Task " + row[0] + " is either in status Requested, Queued, Running, or does not exist on this system.\n")
except GvmResponseError as gvmerr:
print(f"{gvmerr=}, task: {task_start}")
pass
csvFile.close() #close the csv file
except IOError as e:
error_and_exit(f"Failed to read task_file: {str(e)} (exit)")
return numbertasks
def main(gmp: Gmp, args: Namespace) -> None:
# pylint: disable=undefined-variable
if args.script:
args = args.script[1:]
parsed_args = parse_args(args=args)
print(
"Starting tasks.\n"
)
numbertasks = start_tasks(
gmp,
parsed_args.task_file,
)
numbertasks = str(numbertasks)
print(" \n [" + numbertasks + "] task(s)/scan(s) started!\n")
if __name__ == "__gmp__":
main(gmp, args)