MCPcopy Create free account
hub / github.com/achillean/shodan-python / parse

Function parse

shodan/__main__.py:380–437  ·  view source on GitHub ↗

Extract information out of compressed JSON files.

(color, fields, filters, filename, separator, filenames)

Source from the content-addressed store, hash-verified

378@click.option('--separator', help='The separator between the properties of the search results.', default=u'\t')
379@click.argument('filenames', metavar='<filenames>', type=click.Path(exists=True), nargs=-1)
380def parse(color, fields, filters, filename, separator, filenames):
381 """Extract information out of compressed JSON files."""
382 # Strip out any whitespace in the fields and turn them into an array
383 fields = [item.strip() for item in fields.split(',')]
384
385 if len(fields) == 0:
386 raise click.ClickException('Please define at least one property to show')
387
388 has_filters = len(filters) > 0
389
390 # Setup the output file handle
391 fout = None
392 if filename:
393 # If no filters were provided raise an error since it doesn't make much sense w/out them
394 if not has_filters:
395 raise click.ClickException('Output file specified without any filters. Need to use filters with this option.')
396
397 # Add the appropriate extension if it's not there atm
398 if not filename.endswith('.json.gz'):
399 filename += '.json.gz'
400 fout = helpers.open_file(filename)
401
402 for banner in helpers.iterate_files(filenames):
403 row = u''
404
405 # Validate the banner against any provided filters
406 if has_filters and not match_filters(banner, filters):
407 continue
408
409 # Append the data
410 if fout:
411 helpers.write_banner(fout, banner)
412
413 # Loop over all the fields and print the banner as a row
414 for i, field in enumerate(fields):
415 tmp = u''
416 value = get_banner_field(banner, field)
417 if value:
418 field_type = type(value)
419
420 # If the field is an array then merge it together
421 if field_type == list:
422 tmp = u';'.join(value)
423 elif field_type in [int, float]:
424 tmp = u'{}'.format(value)
425 else:
426 tmp = escape_data(value)
427
428 # Colorize certain fields if the user wants it
429 if color:
430 tmp = click.style(tmp, fg=COLORIZE_FIELDS.get(field, 'white'))
431
432 # Add the field information to the row
433 if i > 0:
434 row += separator
435 row += tmp
436
437 click.echo(row)

Callers

nothing calls this directly

Calls 4

match_filtersFunction · 0.90
get_banner_fieldFunction · 0.90
escape_dataFunction · 0.90
getMethod · 0.80

Tested by

no test coverage detected