-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcollect_users.py
More file actions
71 lines (53 loc) · 2.37 KB
/
Copy pathcollect_users.py
File metadata and controls
71 lines (53 loc) · 2.37 KB
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
#! /usr/bin/python3
# import MongoClient class
import pymongo
from pymongo import MongoClient
from pymongo.errors import ConnectionFailure
# ask for variables
defaultHost = "127.0.0.1"
MongoHost = input ("MongoDB host connection details (default 127.0.0.1): ")
if not MongoHost:
MongoHost = defaultHost
defaultPort = 27017
MongoPort = int(input("MongoDB host port (default 27017): ") or defaultPort)
MongoUser = input ("MongoDB admin user: ")
while len(MongoUser)==0:
MongoUser=input("Admin user required - please enter MongoDB admin user: ")
MongoPass = input ("MongoDB password: ")
while len(MongoPass)==0:
MongoPass=input("Password is required - please enter MongoDB password: ")
print("Attempting to connect to MongoDB instance" + " " + MongoHost)
def connMongo():
try:
# create an instance of MongoClient()
conn = MongoClient(host = MongoHost + ":" + str(MongoPort),
serverSelectionTimeoutMS = 5000, # 5 second timeout
username = MongoUser,
password = MongoPass)
conn.server_info()
except (pymongo.errors.ConnectionFailure, pymongo.errors.NetworkTimeout, pymongo.errors.OperationFailure) as e:
print ("Error connecting to server: %s" % e)
return
return conn
def getUsers(client = connMongo()):
if client is None:
return
try:
# authenticating against the admin databse to get user credentials
db = client.admin
userInfo = db.command('usersInfo')
# creating the output file
print("Creating results file")
f = open("user_output.txt", "w+")
print("use admin", file=f)
# iterate through the results of users and output their dbs and roles to the output file
print("Collecting user information")
for document in userInfo['users']:
roleCount = len(document['roles'])
for i in range(roleCount):
print ("db.createUser({user:" + '"' + document['user'] + '",pwd:' + '"{REPLACE THIS VALUE WITH YOUR PASSWORD}",roles:[{"db":"' + document['roles'][i]['db'] +'","role":"' + document['roles'][i]['role'] + '"}]})', file=f)
f.close()
print("User collection successful.")
except:
print ("No host found - check host name")
getUsers()