-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateusers16.py
More file actions
73 lines (60 loc) · 2.43 KB
/
Copy pathcreateusers16.py
File metadata and controls
73 lines (60 loc) · 2.43 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
72
73
#!/usr/bin/python
#-*- coding: utf-8 -*-
# Copyright (C) 2016 Vasilis Daloukas <bdaloukas@gmail.com>
# License GNU GPL version 3 or newer <http://gnu.org/licenses/gpl.html>
import pwd, grp, os, crypt, os, getpass, stat, shutil
def createUser(name, username, password):
encPass = crypt.crypt(password,"22")
cmd = "useradd -p "+encPass+ " -s "+ "/bin/bash "+ "-d "+ "/home/" + username+ " -m "+ " -c \""+ name+"\" " + username
print cmd
return os.system( cmd)
#Δημιουργεί ένα group με το όνομα groupname
def create_group( groupname, groups):
found = 0
for group in groups:
if group[0] == groupname:
found = 1
if found == 0:
print "Create group " + groupname
os.system("groupadd " + groupname)
return found
def append_user_to_group( username, groupname, group):
if username not in group[3]:
print "append " + username + " to group " + groupname
os.system("usermod -a -G " + groupname + " " + username)
#Φτιάχνω τους χρήστες
def create_users(groupname, frompc, topc):
#usernames = create_usernames( computers, tmimata)
#print usernames
#Αρχικά φτιάχνω το group με όνομα groupname
try:
groups = grp.getgrall()
except:
raise GroupDatabaseException(_("Failed to get the group list"))
create_group( groupname, groups)
#Πρέπει να φτιάξω ένα group ανά τμήμα
#for tmima in tmimata:
# if tmima != '':
# create_group( tmima, groups)
#Πρέπει να ξαναδιαβάσω τα groups γιατί έφτιαξα και νέα
try:
groups = grp.getgrall()
except:
raise GroupDatabaseException(_("Failed to get the group list"))
#Στη συνέχεια φτιάχνω τους χρήστες
users = []
usernames = ['administrator']
for p in pwd.getpwall():
users.append( p[0])
for i in range(frompc, topc+1):
password = str( i / 10) + str( i % 10)
username = "pc" + password
usernames.append( username)
if( username not in users):
createUser( username, username, password)
#Βάζω τους χρήστες στο κατάλληλο group
for group in groups:
if group[0] == groupname:
for username in usernames:
append_user_to_group( username, groupname, group)
create_users( "erg", 1, 15)