Zentyal Forum, Linux Small Business Server
Zentyal Server => Installation and Upgrades => Topic started by: julio on November 09, 2014, 04:00:57 pm
-
import-users (modified 16.09.2015)
#!/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)
#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!
-
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,
-
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)
ls -la
-
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:
root@zentyal:~# perl add_user.pl
readline() on closed filehandle $USERS at add_user.pl line 17.
users.csv is:
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
-
my results:
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"
cat add_user.pl > results
cat users.csv >> results
ls -la >> results
Please post the "results" file.
-
Thanks for answer!
here is result:
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?
-
i have only the default installed perl modules.
please change this line:
open (my $USERS, 'users.csv');
to
open(my $USERS, '<', 'users.csv') or die "Error opening 'users.csv' file: $!";
-
thanks!!! that pointed me to error:
Error opening 'users.csv' file: Permission denied at add_user.pl line 16.
i moved script and csv in /tmp/ and all is works!
-
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? :)
-
Hi iMongui,
please create a group (common) first!
-
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
-
Please check your "users.csv", all comma at the right place? man :D
-
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.
-
hi julio,
Your code work for me.
Thank you. :)
-
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,
-
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!
-
There is any place where found a referece about the perl modules?
-
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.
-
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
--- 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;
-
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