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