Zentyal Forum, Linux Small Business Server

Zentyal Server => Installation and Upgrades => Topic started by: julio on November 09, 2014, 04:00:57 pm

Title: New!! Bulk User import for Zentyal 4 with add to existing OU/Group function
Post by: julio on November 09, 2014, 04:00:57 pm
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!
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: sergiobaiao 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,
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: julio 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
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: mrpsycho 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
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: julio 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.
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: mrpsycho 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?
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: julio 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: $!";
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: mrpsycho 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!
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: iMongui 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? :)
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: julio on December 15, 2014, 01:25:32 am
Hi iMongui,

please create a group (common) first!
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: iMongui 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
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: julio on December 18, 2014, 07:44:10 pm
Please check your "users.csv", all comma at the right place? man :D
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: adamb 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. 
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: pingpong51 on May 24, 2015, 05:05:11 am
hi julio,

Your code work for me.

Thank you.  :)
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: Rendiok 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,
Title: Re: Bulk User import for Zentyal 4 with add to existing Group function
Post by: julio on September 16, 2015, 09:51:01 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,

i've modified the script and the users.csv structure, please test it!
Title: Re: New!! Bulk User import for Zentyal 4 with add to existing OU/Group function
Post by: cstfff on March 05, 2016, 04:50:03 pm
There is any place where found a referece about the perl modules?
Title: Re: New!! Bulk User import for Zentyal 4 with add to existing OU/Group function
Post by: elphio on June 28, 2016, 01:42:12 pm
On a fresh installation (v 4.2) all up to date, only Domain Controller and File Sharing (and it's dipendencies) confugured, the script (with the users.csv in the example) give me the "Missing argument: parent" error.
Title: Re: New!! Bulk User import for Zentyal 4 with add to existing OU/Group function
Post by: Arif Ali on August 29, 2016, 12:39:04 am
On a fresh installation (v 4.2) all up to date, only Domain Controller and File Sharing (and it's dipendencies) confugured, the script (with the users.csv in the example) give me the "Missing argument: parent" error.

I have just tried it and I had to make the following modification to get it working, try the change and see if that works

Code: [Select]
--- bulk_add_users.pl.orig      2016-08-28 23:38:05.240111912 +0100
+++ bulk_add_users.pl   2016-08-28 23:32:54.596560475 +0100
@@ -18,7 +18,7 @@
   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 $parent = $usersMod->objectFromDN("cn=$ou," . $usersMod->ldap->dn());
 
     my %user;
     $user{parent}         = $parent;
Title: Re: New!! Bulk User import for Zentyal 4 with add to existing OU/Group function
Post by: harshkukreja on September 07, 2016, 12:00:28 pm
Hi

I like to use the script for importing bulk users from the csv but I like to add the E-mail field also. Please help with the changes