-
Notifications
You must be signed in to change notification settings - Fork 0
/
anewdb.py
81 lines (70 loc) · 2.01 KB
/
anewdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import argparse
import json
import sys
from pymongo import MongoClient
client = MongoClient('mongodb://root:example@localhost:27017/')
db = client.mydb
def add(collection, domain, data, pk):
try:
db[collection].replace_one(
{'_id': data[f'{pk}']},
{'domain': domain, 'data': data},
upsert=True
)
print(f'[INFO] - data add - {data}')
except:
print(f'[ERROR] - fail to add data to DB - {data}')
def parser_argumensts():
parser = argparse.ArgumentParser(description="Data Manage")
parser.add_argument(
"-t",
"--tool",
required=True,
help="Tool kind that define the collection (httpx, katana, dnsx, etc.)"
)
parser.add_argument(
"command",
choices=["add", "get", "del"],
help="Commands (add, get, del)"
)
parser.add_argument(
"-dt",
"--data",
help="JSON data used with add"
)
parser.add_argument(
"-d",
"--domain",
help="Domain (target.com)"
)
parser.add_argument(
"-k",
"--key",
help="Key that will prevent duplicated in DB"
)
parser.add_argument(
"-q",
"--query",
help="Query JSON (used with 'get' and 'del')"
)
return parser.parse_args()
if __name__ == "__main__":
args = parser_argumensts()
if args.command == "add":
if args.data:
try:
data = json.loads(args.data)
except:
data = None
print(f'[ERROR] - stdin data is not a valid json: {args.data}')
if data:
add(args.tool, args.domain, data, args.key)
else:
for line in sys.stdin:
try:
data = json.loads(line)
except:
data = None
print(f'[ERROR] - stdin data is not a valid json: {line}')
if data:
add(args.tool, args.domain, data, args.key)