Skip to content

Instantly share code, notes, and snippets.

@fn-alves
Last active April 17, 2019 13:04
Show Gist options
  • Save fn-alves/ca89c091578f8d065a9bcaf50a398245 to your computer and use it in GitHub Desktop.
Save fn-alves/ca89c091578f8d065a9bcaf50a398245 to your computer and use it in GitHub Desktop.
Importa usuários contidos no csv em massa para o samba 4 AD. headers do csv: nome,sobrenome,email,senha,departamento,empresa
#!/usr/bin/env python
import csv, sys,os
try:
file = open(sys.argv[1], "rt")
reader = csv.DictReader(file)
# Function que adiciona os usuairos no samba e define para mudar a senha no proximo login
def add_user(username,senha,email,sobrenome,nome,descr,departamento,empresa,user_ou):
os.system("/usr/bin/samba-tool user add %s \
%s \
--mail-address=%s \
--surname='%s' \
--given-name='%s' \
--description='%s' \
--department='%s' \
--company='%s' \
--userou=%s"\
% (username,senha,email,sobrenome,nome,descr,departamento,empresa,user_ou))
os.system("/usr/bin/samba-tool user setpassword \
--newpassword=%s \
--must-change-at-next-login \
--filter=samaccountname=%s" \
% (senha,username))
return
for row in reader:
# Popula as variaveis com os dados do csv
email = row['email']
username = email.split('@')[0]
senha = row['senha']
nome = row['nome']
sobrenome = row['sobrenome']
departamento = row['departamento']
empresa = row['empresa']
seq = "_"
sp_empresa = empresa.split()
empresa_ou = seq.join(sp_empresa)
user_ou = 'OU=Usuarios,OU=%s,OU=Departamentos,OU=%s,OU=Estrutura_Corporativa' % (departamento,empresa_ou)
descr = 'Emp: %s - %s' % (empresa, departamento)
# Chama a function add user passando os parametros
add_user(username,senha,email,sobrenome,nome,descr,departamento,empresa,user_ou)
except IOError as (errno, strerror):
print "I/O error({0}): {1}".format(errno,strerror)
finally:
file.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment