| Conditions | 4 |
| Total Lines | 147 |
| Code Lines | 98 |
| 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 | config = parser.add_mutually_exclusive_group() |
||
| 288 | |||
| 289 | config.add_argument( |
||
| 290 | "+C", |
||
| 291 | "++scan_config", |
||
| 292 | default=0, |
||
| 293 | type=int, |
||
| 294 | dest='config', |
||
| 295 | help="Choose from existing scan config:" |
||
| 296 | " 0: Full and fast" |
||
| 297 | " 1: Full and fast ultimate" |
||
| 298 | " 2: Full and very deep" |
||
| 299 | " 3: Full and very deep ultimate" |
||
| 300 | " 4: System Discovery", |
||
| 301 | ) |
||
| 302 | |||
| 303 | config.add_argument( |
||
| 304 | "++scan_config_id", |
||
| 305 | default=0, |
||
| 306 | type=int, |
||
| 307 | dest='scan_config_id', |
||
| 308 | help="Use existing scan config by id", |
||
| 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 | # set alert_name to recipient email if no other name |
||
| 337 | # is given |
||
| 338 | if script_args.alert_name is None: |
||
| 339 | script_args.alert_name = script_args.recipient_email |
||
| 340 | |||
| 341 | # use existing config from argument |
||
| 342 | if not script_args.scan_config_id: |
||
| 343 | config_id = get_config(gmp, script_args.config) |
||
| 344 | else: |
||
| 345 | config_id = script_args.scan_config_id |
||
| 346 | |||
| 347 | # create new target or use existing one from id |
||
| 348 | if not script_args.target_id: |
||
| 349 | target_id = get_target( |
||
| 350 | gmp, |
||
| 351 | target_name=script_args.target_id, |
||
| 352 | hosts=script_args.hosts, |
||
| 353 | ports=script_args.ports, |
||
| 354 | port_list_name=script_args.port_list_name, |
||
| 355 | port_list_id=script_args.port_list_id, |
||
| 356 | ) |
||
| 357 | else: |
||
| 358 | target_id = script_args.target_id |
||
| 359 | alert_id = get_alert( |
||
| 360 | gmp, |
||
| 361 | script_args.sender_email, |
||
| 362 | script_args.recipient_email, |
||
| 363 | script_args.alert_name, |
||
| 364 | ) |
||
| 365 | scanner_id = get_scanner(gmp) |
||
| 366 | |||
| 367 | create_and_start_task( |
||
| 368 | gmp, config_id, target_id, scanner_id, alert_id, script_args.alert_name |
||
| 369 | ) |
||
| 370 | |||
| 371 | print("\nScript finished\n") |
||
| 372 | |||
| 376 |