Commit a9bc9953 authored by Alexandre Dulaunoy's avatar Alexandre Dulaunoy
Browse files

JSON output added to CPE search

parent fe07e595
Loading
Loading
Loading
Loading
+14 −2
Original line number Original line Diff line number Diff line
@@ -5,6 +5,7 @@
#
#
# Copyright (c) 2014 	psychedelys
# Copyright (c) 2014 	psychedelys
# Copyright (c) 2015 	Pieter-Jan Moreels - pieterjan.moreels@gmail.com
# Copyright (c) 2015 	Pieter-Jan Moreels - pieterjan.moreels@gmail.com
# Copyright (c) 2015    Alexandre Dulaunoy - a@foo.be


# Imports
# Imports
import os
import os
@@ -14,6 +15,7 @@ sys.path.append(os.path.join(runPath, ".."))


import re
import re
import argparse
import argparse
import json


from lib.Config import Configuration
from lib.Config import Configuration
import lib.DatabaseLayer as db
import lib.DatabaseLayer as db
@@ -24,7 +26,7 @@ vOutput = ""


argParser = argparse.ArgumentParser(description='Search for CPE with a pattern')
argParser = argparse.ArgumentParser(description='Search for CPE with a pattern')
argParser.add_argument('-s', type=str, required=True, help='search in cpe list')
argParser.add_argument('-s', type=str, required=True, help='search in cpe list')
argParser.add_argument('-o', type=str, help='O = output format [compact]')
argParser.add_argument('-o', type=str, default='expanded' ,help='O = output format [expanded, compact, json] (default: expanded)')
argParser.add_argument('-f', action='store_true', help='Enlarge the CPE search to all CPE indexed. Need the cpeother activated.', default=False)
argParser.add_argument('-f', action='store_true', help='Enlarge the CPE search to all CPE indexed. Need the cpeother activated.', default=False)


args = argParser.parse_args()
args = argParser.parse_args()
@@ -38,9 +40,19 @@ def search(cpe):
    if vOutput == "compact":
    if vOutput == "compact":
        for item in res:
        for item in res:
            print(item['id'])
            print(item['id'])
    else:
    elif vOutput == "expanded":
        for item in res:
        for item in res:
            print(item['id'] + "  " + item['title'])
            print(item['id'] + "  " + item['title'])
    elif vOutput == "json":
        o = []
        for item in res:
            x = {}
            x['id'] = item['id']
            x['title'] = item['title']
            o.append(x)
        print(json.dumps(o, sort_keys=True, indent=4))




# replace special characters in cpeSearch with encoded version.
# replace special characters in cpeSearch with encoded version.
cpeSearch = re.sub(r'\(', '%28', cpeSearch)
cpeSearch = re.sub(r'\(', '%28', cpeSearch)