Author Topic: New!! Bulk User import for Zentyal 4 with add to existing OU/Group function  (Read 13193 times)

julio

  • Guest
import-users (modified 16.09.2015)
Code: [Select]
#!/usr/bin/perl

use strict;
use warnings;

use EBox;
use EBox::Samba;
use EBox::Samba::User;
use EBox::Samba::Group;

EBox::init();

my $usersMod = EBox::Global->modInstance('samba');

open(my $users, '<', 'users.csv') or die "Error opening 'users.csv' file: $!";

while (my $line = <$users>) {
  chomp ($line);
  if (substr($line, 0, 1) ne '#') {
    my ($ou, $group, $username, $firstname, $lastname, $password, $description) = split(',', $line);
    my $parent = $usersMod->objectFromDN("ou=$ou," . $usersMod->ldap->dn());

    my %user;
    $user{parent}         = $parent;
    $user{group}          = $group;
    $user{samAccountName} = $username;
    $user{givenName}      = $firstname;
    $user{sn}             = $lastname;
    $user{password}       = $password;
    $user{description}    = $description; #optional

    my $nuser = EBox::Samba::User->create(%user);
    if ($nuser->exists()) {
      print "$username added to ou:$ou\n";
      if ($group ne '-') {
        $nuser->addGroup(new EBox::Samba::Group(samAccountName => $user{group}));
        if ($nuser->exists()) {
          print "$username added to ou:$ou/group:$group\n";
        }
      }
    }
  }
}

close ($users);
1;

users.csv (modified 16.09.2015)
Code: [Select]
#ou(existing),group(existing),username,firstname,lastname,password,description(optional)
ou,group,username1,firstname1,lastname1,password1,description1
ou,-,username2,firstname2,lastname2,password2,description2

New!! if group='-' user not added to any group!
« Last Edit: September 16, 2015, 10:42:53 pm by julio »

sergiobaiao

  • Zen Apprentice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #1 on: November 22, 2014, 09:21:15 pm »
could you make it work? here it gives and error:

readline() on closed filehandle $USERS at ./bulkimport line 16.

users.csv is at the same folder, with this content:

CHAT,user,givenName,surName,password,

julio

  • Guest
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #2 on: November 23, 2014, 12:06:11 pm »
Sorry, but it works for me...

your user.csv content:
CHAT,user,givenName,surName,password,description  ??

if you don't need description, please no comma at end of line:
CHAT,user,givenName,surName,password

Please send the result of the following command:

go to yourdirectory (import-users, users.csv)
Code: [Select]
ls -la
« Last Edit: November 23, 2014, 12:13:48 pm by julio »

mrpsycho

  • Zen Apprentice
  • *
  • Posts: 47
  • Karma: +4/-1
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #3 on: November 27, 2014, 12:10:22 pm »
Hello!

i've tried your script - and have next error:
root@zentyal:~# perl users.csv
Can't locate object method "blahblah" via package "common" (perhaps you forgot to load "common"?) at users.csv line 2.

my mistake...

actual error is the same:
Code: [Select]
root@zentyal:~# perl add_user.pl
readline() on closed filehandle $USERS at add_user.pl line 17.


users.csv is:
Code: [Select]
root@zentyal:~# cat users.csv
#group(existing),username,firstname,lastname,password,description(optional)
common,test1,testName1,testLast1,sdfsdf,blahblah
common,test2,testName2,testLast2,sdfggg,Hehe
« Last Edit: November 27, 2014, 12:51:37 pm by mrpsycho »

julio

  • Guest
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #4 on: November 27, 2014, 05:50:13 pm »
my results:
Code: [Select]
test1 added
test1 added to common
test2 added
test2 added to common

your possible error is:
1. "add_user.pl" and "users.csv" not in the same directory
or/and
2. you do not have sufficient rights to open the "users.csv"

Code: [Select]
cat add_user.pl > results
cat users.csv >> results
ls -la >> results
Please post the "results" file.
« Last Edit: November 27, 2014, 06:27:14 pm by julio »

mrpsycho

  • Zen Apprentice
  • *
  • Posts: 47
  • Karma: +4/-1
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #5 on: November 28, 2014, 02:46:25 pm »
Thanks for answer!

here is result:

Code: [Select]
root@zentyal:~# cat results
#!/usr/bin/perl

use strict;
use warnings;

use EBox;
use EBox::Samba;
use EBox::Samba::User;
use EBox::Samba::Group;

EBox::init();

my $parent = EBox::Samba::User->defaultContainer();

open (my $USERS, 'users.csv');

while (my $line = <$USERS>) {
  chomp ($line);
  if (substr($line, 0, 1) ne '#') {
    my ($group, $username, $firstname, $lastname, $password, $description) = split(',', $line);

    my %user;
    $user{parent}         = $parent;
    $user{group}          = $group;
    $user{samAccountName} = $username;
    $user{givenName}      = $firstname;
    $user{sn}             = $lastname;
    $user{password}       = $password;
    $user{description}    = $description; #optional

    my $nuser = EBox::Samba::User->create(%user);
    if ($nuser->exists()) {
      print "$username added\n";
      $nuser->addGroup(new EBox::Samba::Group(samAccountName => $user{group}));
      if ($nuser->exists()) {
        print "$username added to $group\n";
      }
    }
  }
}

close ($USERS);

1;
#group(existing),username,firstname,lastname,password,description(optional)
common,test1,testName1,testLast1,sdfsdf,blahblah
common,test2,testName2,testLast2,sdfggg,Hehe


total 80
drwx------  9 root root 4096 Nov 28 16:42 .
drwxr-xr-x 23 root root 4096 Nov 27 12:44 ..
-rw-r--r--  1 root root 1003 Nov 27 14:36 add_user.pl
-rw-------  1 root root  607 Nov 27 18:11 .bash_history
-rw-r--r--  1 root root 3106 Feb 20  2014 .bashrc
drwx------  3 root root 4096 Nov 27 12:54 .cache
drwx------  4 root root 4096 Nov 27 15:51 .config
drwx------  3 root root 4096 Nov 27 12:31 .dbus
drwx------  2 root root 4096 Nov 27 12:31 .gconf
drwx------  3 root root 4096 Nov 27 12:31 .gnome2
drwx------  2 root root 4096 Nov 27 12:31 .gnome2_private
drwx------  3 root root 4096 Nov 27 12:54 .local
-rw-r--r--  1 root root  140 Feb 20  2014 .profile
-rw-r--r--  1 root root 1175 Nov 28 16:42 results
-rw-r--r--  1 root root   72 Nov 27 12:54 .selected_editor
-rw-r--r--  1 root root  172 Nov 27 14:02 users.csv

and it couldn't be problem with permissions, cause i try to do it from root user.

maybe i have to install some extra perl modules?

julio

  • Guest
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #6 on: November 28, 2014, 06:20:03 pm »
i have only the default installed perl modules.

please change this line:
Code: [Select]
open (my $USERS, 'users.csv');to
Code: [Select]
open(my $USERS, '<', 'users.csv') or die "Error opening 'users.csv' file: $!";
« Last Edit: November 28, 2014, 07:33:19 pm by julio »

mrpsycho

  • Zen Apprentice
  • *
  • Posts: 47
  • Karma: +4/-1
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #7 on: December 01, 2014, 10:49:25 am »
thanks!!! that pointed me to error:
Code: [Select]
Error opening 'users.csv' file: Permission denied at add_user.pl line 16.

i moved script and csv in /tmp/ and all is works!

iMongui

  • Zen Apprentice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #8 on: December 14, 2014, 09:10:44 pm »
Hi man !

Nice script, but I have a problem... I made the SAME script and the same users.csv. I executed in the same folder than my mate (/tmp), and i have this:

sudo perl add.pl
perl: warning: Setting locale failed.
perl: warning: Please check that your locale settings:
   LANGUAGE = (unset),
   LC_ALL = (unset),
   LC_CTYPE = "UTF-8",
   LANG = "en_US.UTF-8"
    are supported and installed on your system.
perl: warning: Falling back to the standard locale ("C").
username1 added
Can't call method "add" on an undefined value at /usr/share/perl5/EBox/Samba/LdapObject.pm line 231, <GEN0> line 37.
   ...propagated at /usr/share/perl5/EBox/Samba/Group.pm line 322, <GEN0> line 37.

Any help pls? :)

julio

  • Guest
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #9 on: December 15, 2014, 01:25:32 am »
Hi iMongui,

please create a group (common) first!
« Last Edit: December 15, 2014, 01:27:23 am by julio »

iMongui

  • Zen Apprentice
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #10 on: December 18, 2014, 03:25:24 pm »
Weel, I was able to create users, but now I have another file with the same structure and more users, and I don't know why only create one by one. What can u tell me about it man? :)

Anyways, thanks for the script, you saved my life :D

julio

  • Guest
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #11 on: December 18, 2014, 07:44:10 pm »
Please check your "users.csv", all comma at the right place? man :D

adamb

  • Zen Apprentice
  • *
  • Posts: 4
  • Karma: +0/-0
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #12 on: January 13, 2015, 03:04:42 pm »
We are looking to use this tool but with organizational units.  Is there an easy way to add these to the script?  I appreciate the input, nice job on the script. 

pingpong51

  • Zen Apprentice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #13 on: May 24, 2015, 05:05:11 am »
hi julio,

Your code work for me.

Thank you.  :)

Rendiok

  • Zen Apprentice
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
Re: Bulk User import for Zentyal 4 with add to existing Group function
« Reply #14 on: September 15, 2015, 03:20:27 pm »
Hello Julio,

Thanks for your work. Only two questions, your script puts all users on "default container", if I have an specific OU (for example students), how I have to do to change default container to "students" OU?

And if I need put some users in two groups, can I add more values in the script? Where I can look the possibles values for the script?

Regards,