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 -*- |
||
226 | def main(gmp, args): |
||
227 | # pylint: disable=undefined-variable |
||
228 | |||
229 | parser = ArgumentParser( |
||
230 | prefix_chars="+", |
||
231 | add_help=False, |
||
232 | formatter_class=RawTextHelpFormatter, |
||
233 | description=HELP_TEXT, |
||
234 | ) |
||
235 | |||
236 | parser.add_argument( |
||
237 | "+h", |
||
238 | "++help", |
||
239 | action="help", |
||
240 | help="Show this help message and exit.", |
||
241 | ) |
||
242 | |||
243 | target = parser.add_mutually_exclusive_group(required=True) |
||
244 | |||
245 | target.add_argument( |
||
246 | "++target-id", |
||
247 | type=str, |
||
248 | dest="target_id", |
||
249 | help="Use an existing by target id", |
||
250 | ) |
||
251 | |||
252 | target.add_argument( |
||
253 | "++target-name", |
||
254 | type=str, |
||
255 | dest="target_name", |
||
256 | help="Create a target by name", |
||
257 | ) |
||
258 | |||
259 | parser.add_argument( |
||
260 | "++hosts", |
||
261 | nargs='+', |
||
262 | dest='hosts', |
||
263 | help="Host(s) for the new target", |
||
264 | ) |
||
265 | |||
266 | ports = parser.add_mutually_exclusive_group() |
||
267 | |||
268 | ports.add_argument( |
||
269 | "++port-list-id", |
||
270 | type=str, |
||
271 | dest="port_list_id", |
||
272 | help="An existing portlist id for the new target", |
||
273 | ) |
||
274 | ports.add_argument( |
||
275 | "++ports", |
||
276 | type=str, |
||
277 | dest='ports', |
||
278 | help="Ports in the new target: e.g. T:80-80,8080", |
||
279 | ) |
||
280 | |||
281 | parser.add_argument( |
||
282 | "++port-list-name", |
||
283 | type=str, |
||
284 | dest="port_list_name", |
||
285 | help="Name for the new portlist in the new target", |
||
286 | ) |
||
287 | |||
288 | parser.add_argument( |
||
289 | "+C", |
||
290 | "++scan_config", |
||
291 | default=0, |
||
292 | type=int, |
||
293 | dest='config', |
||
294 | help="Choose from existing scan config:" |
||
295 | " 0: Full and fast" |
||
296 | " 1: Full and fast ultimate" |
||
297 | " 2: Full and very deep" |
||
298 | " 3: Full and very deep ultimate" |
||
299 | " 4: System Discovery", |
||
300 | ) |
||
301 | |||
302 | parser.add_argument( |
||
303 | "++recipient", |
||
304 | required=True, |
||
305 | dest='recipient_email', |
||
306 | type=str, |
||
307 | help="Alert recipient E-Mail address", |
||
308 | ) |
||
309 | |||
310 | parser.add_argument( |
||
311 | "++sender", |
||
312 | required=True, |
||
313 | dest='sender_email', |
||
314 | type=str, |
||
315 | help="Alert senders E-Mail address", |
||
316 | ) |
||
317 | |||
318 | parser.add_argument( |
||
319 | "++alert-name", |
||
320 | dest='alert_name', |
||
321 | type=str, |
||
322 | help="Optional Alert name", |
||
323 | ) |
||
324 | |||
325 | script_args, _ = parser.parse_known_args() |
||
326 | |||
327 | if script_args.alert_name is None: |
||
328 | script_args.alert_name = script_args.recipient_email |
||
329 | |||
330 | config_id = get_config(gmp, script_args.config) |
||
331 | if not script_args.target_id: |
||
332 | target_id = get_target( |
||
333 | gmp, |
||
334 | target_name=script_args.target_id, |
||
335 | hosts=script_args.hosts, |
||
336 | ports=script_args.ports, |
||
337 | port_list_name=script_args.port_list_name, |
||
338 | port_list_id=script_args.port_list_id, |
||
339 | ) |
||
340 | else: |
||
341 | target_id = script_args.target_id |
||
342 | alert_id = get_alert( |
||
343 | gmp, |
||
344 | script_args.sender_email, |
||
345 | script_args.recipient_email, |
||
346 | script_args.alert_name, |
||
347 | ) |
||
348 | scanner_id = get_scanner(gmp) |
||
349 | |||
350 | create_and_start_task( |
||
351 | gmp, config_id, target_id, scanner_id, alert_id, script_args.alert_name |
||
352 | ) |
||
353 | |||
354 | print("\nScript finished\n") |
||
355 | |||
359 |