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