-
Notifications
You must be signed in to change notification settings - Fork 0
/
pySearchPassDB.py
72 lines (60 loc) · 1.84 KB
/
pySearchPassDB.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
import pymongo
import os
import re
import sys
import argparse
coll = pymongo.MongoClient("localhost", 27017)["pySearchPassDB"]["pySearchPassDB"]
PATTERN_SPLIT = (':', ';')
#Add fields to db
def addToDB(dir):
for files in os.walk(dir):
for file in files[2]:
with open(files[0] + "/" + file, errors='ignore') as f:
for line in f:
for pattern in PATTERN_SPLIT:
arrLine = re.split(pattern, line.strip('\n'), maxsplit=1)
if len(arrLine) == 2:
break
else:
print("Error: string '" + str(arrLine[0].strip('\n')) + "' does not parse")
continue
arrLine[0] = arrLine[0].lower()
coll.update({"_id": arrLine[0]}, { '$addToSet': { "pass": arrLine[1]} }, upsert=True)
#Clear all db fields
def clearDB():
coll.remove({})
#Search email
def searchEmail(email):
return coll.find_one({"_id": email.lower()})
#Search email and convenient output
def printSearchEmail(email):
res = searchEmail(email)
if res:
print("email:" + res["_id"])
if len(res["pass"]) > 1:
print("passwords:")
for p in res["pass"]:
print(p)
else:
print("password:" + res["pass"][0])
else:
print("Not found")
#Get count fileds of db
def getCountEmails():
return coll.count()
#Parse arguments
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--search', help='Search email', metavar = 'EMAIL')
parser.add_argument('-a', '--add', help='Add fields to db', metavar = 'PATH')
parser.add_argument('-c', '--count', action='store_true', help='Show count fileds of db')
parser.add_argument('--clear', action='store_true', help='Clear all db fields')
if parser.parse_args().count:
print(getCountEmails())
if parser.parse_args().search:
printSearchEmail(parser.parse_args().search)
if parser.parse_args().clear:
clearDB()
if parser.parse_args().add:
addToDB(parser.parse_args().add)
if len(sys.argv) == 1:
parser.print_help()