Conditions | 3 |
Total Lines | 129 |
Code Lines | 89 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
225 | def main(gmp, args): |
||
226 | # pylint: disable=undefined-variable, unused-argument |
||
227 | |||
228 | parser = ArgumentParser( |
||
229 | prefix_chars="+", |
||
230 | add_help=False, |
||
231 | formatter_class=RawTextHelpFormatter, |
||
232 | description=HELP_TEXT, |
||
233 | ) |
||
234 | |||
235 | parser.add_argument( |
||
236 | "+h", |
||
237 | "++help", |
||
238 | action="help", |
||
239 | help="Show this help message and exit.", |
||
240 | ) |
||
241 | |||
242 | target = parser.add_mutually_exclusive_group(required=True) |
||
243 | |||
244 | target.add_argument( |
||
245 | "++target-id", |
||
246 | type=str, |
||
247 | dest="target_id", |
||
248 | help="Use an existing by target id", |
||
249 | ) |
||
250 | |||
251 | target.add_argument( |
||
252 | "++target-name", |
||
253 | type=str, |
||
254 | dest="target_name", |
||
255 | help="Create a target by name", |
||
256 | ) |
||
257 | |||
258 | parser.add_argument( |
||
259 | "++hosts", |
||
260 | nargs='+', |
||
261 | dest='hosts', |
||
262 | help="Host(s) for the new target", |
||
263 | ) |
||
264 | |||
265 | ports = parser.add_mutually_exclusive_group() |
||
266 | |||
267 | ports.add_argument( |
||
268 | "++port-list-id", |
||
269 | type=str, |
||
270 | dest="port_list_id", |
||
271 | help="An existing portlist id for the new target", |
||
272 | ) |
||
273 | ports.add_argument( |
||
274 | "++ports", |
||
275 | type=str, |
||
276 | dest='ports', |
||
277 | help="Ports in the new target: e.g. T:80-80,8080", |
||
278 | ) |
||
279 | |||
280 | parser.add_argument( |
||
281 | "++port-list-name", |
||
282 | type=str, |
||
283 | dest="port_list_name", |
||
284 | help="Name for the new portlist in the new target", |
||
285 | ) |
||
286 | |||
287 | parser.add_argument( |
||
288 | "+C", |
||
289 | "++scan_config", |
||
290 | default=0, |
||
291 | type=int, |
||
292 | dest='config', |
||
293 | help="Choose from existing scan config:" |
||
294 | " 0: Full and fast" |
||
295 | " 1: Full and fast ultimate" |
||
296 | " 2: Full and very deep" |
||
297 | " 3: Full and very deep ultimate" |
||
298 | " 4: System Discovery", |
||
299 | ) |
||
300 | |||
301 | parser.add_argument( |
||
302 | "++recipient", |
||
303 | required=True, |
||
304 | dest='recipient_email', |
||
305 | type=str, |
||
306 | help="Alert recipient E-Mail address", |
||
307 | ) |
||
308 | |||
309 | parser.add_argument( |
||
310 | "++sender", |
||
311 | required=True, |
||
312 | dest='sender_email', |
||
313 | type=str, |
||
314 | help="Alert senders E-Mail address", |
||
315 | ) |
||
316 | |||
317 | parser.add_argument( |
||
318 | "++alert-name", |
||
319 | dest='alert_name', |
||
320 | type=str, |
||
321 | help="Optional Alert name", |
||
322 | ) |
||
323 | |||
324 | script_args, _ = parser.parse_known_args() |
||
325 | |||
326 | if script_args.alert_name is None: |
||
327 | script_args.alert_name = script_args.recipient_email |
||
328 | |||
329 | config_id = get_config(gmp, script_args.config) |
||
330 | if not script_args.target_id: |
||
331 | target_id = get_target( |
||
332 | gmp, |
||
333 | target_name=script_args.target_id, |
||
334 | hosts=script_args.hosts, |
||
335 | ports=script_args.ports, |
||
336 | port_list_name=script_args.port_list_name, |
||
337 | port_list_id=script_args.port_list_id, |
||
338 | ) |
||
339 | else: |
||
340 | target_id = script_args.target_id |
||
341 | alert_id = get_alert( |
||
342 | gmp, |
||
343 | script_args.sender_email, |
||
344 | script_args.recipient_email, |
||
345 | script_args.alert_name, |
||
346 | ) |
||
347 | scanner_id = get_scanner(gmp) |
||
348 | |||
349 | create_and_start_task( |
||
350 | gmp, config_id, target_id, scanner_id, alert_id, script_args.alert_name |
||
351 | ) |
||
352 | |||
353 | print("\nScript finished\n") |
||
354 | |||
358 |