-
Notifications
You must be signed in to change notification settings - Fork 0
/
userAdd.py
56 lines (46 loc) · 2.05 KB
/
userAdd.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
#!/usr/bin/env python
import Globals
import string
import random
from Products.ZenUtils.ZenScriptBase import ZenScriptBase
from transaction import commit
from sys import argv
#This script takes user email addresses as arguments to the script in the form [email protected],GroupName OR [email protected],'Group Name'
dmd = ZenScriptBase(connect=True).dmd
#Generates a random password for the user
def passgen(size=8, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
for i in range(1,len(argv)):
password = passgen()
#Var for referenceable list
j = argv[i].split(",")
#Var for user's ID. If this is None, we'll add the user and add them to the group after the comma. If the group name has a space, enclose the group name in quotes.
k = dmd.ZenUsers.getUser(str(j[0]))
if k is None:
#Password has been generated, now add the user as a ZenManager.
if len(j) == 1:
#If only the email address is entered, make ZenUser and don't add to groups.
dmd.ZenUsers.manage_addUser(str(j[0]),password,email=str(j[0]))
#Print the results so we can copy/paste the password to the user.
print argv[i],password
elif len(j) == 2:
if str(j[1]).startswith('Zen') == True:
#Add to ROLE INSTEAD
dmd.ZenUsers.manage_addUser(str(j[0]),password,email=str(j[0]),roles=str(j[1]))
else:
dmd.ZenUsers.manage_addUser(str(j[0]),password,email=str(j[0]))
print "Adding user to group..."
dmd.ZenUsers.manage_addUsersToGroups(str(j[0]),str(j[1]))
elif len(j) == 3:
dmd.ZenUsers.manage_addUser(str(j[0]),password,email=str(j[0]),roles=str(j[1]))
dmd.ZenUsers.manage_addUsersToGroups(str(j[0]),str(j[2]))
else:
print "Syntax Error"
print "Please use the format [email protected] OR [email protected],ZenManager OR"
print "[email protected],GroupName OR [email protected],ZenManager,GroupName."
print "Role options are ZenUser, ZenManager, or ZenOperator."
print "If a group name has a space in it, be sure to enclose it in quotes."
commit()
else:
print "User already exists! Skipping..."
print "Done!"