Zentyal Forum, Linux Small Business Server
Zentyal Server => Installation and Upgrades => Topic started by: tuxmania on September 09, 2010, 09:13:28 am
-
Hi,
Im tinkering a bit with Zentyal in advance of our deployment. I managed to add users to more than one group and thought i would toss it here if someone would ever want to do that:
The script is called groupadd.pl but you can name it anything.pl I had trouble running it from /root because of permissions but it works if i put it in my /home/adminuser dir and run it with sudo.
#!/usr/bin/perl
use strict;
use warnings;
use EBox;
use EBox::Global;
EBox::init();
my $usersModule = EBox::Global->modInstance('users');
my @users = ();
open(my $USERS,"users") or die "Error opening 'users' file: $!";
while(my $line = <$USERS>) {
chomp ($line);
my $user;
my ($username,$givenname,$surname,$password,$group1, $group2) = split(',',$line);
$user->{'user'} = $username;
$user->{'givenname'} = $givenname;
$user->{'surname'} = $surname;
$user->{'password'} = $password;
print "\n Creating user: $username $givenname $surname \n";
$usersModule->addUser($user,0);
$usersModule->addUserToGroup($username, $group1);
$usersModule->addUserToGroup($username, $group2);
}
close($USERS);
1;
The script uses a csv formatted file that has to be in the same path and be called users This is its format:
Username1,Firstname1,Lastname1,Password1, Firstgroup1, Secondgroup1
Username2,Firstname2,Lastname2,Password2, Firstgroup2, Secondgroup2
etc...
-
Thanks for sharing this tuxmania!
Great script
-
Yes, the script is great but not mine, i just changed someone elses.
-
just for the record, here is my version which uses the API somewhat different and does ANY number of groups
#!/usr/bin/perl
use strict;
use warnings;
use EBox;
use EBox::UsersAndGroups::User;
use EBox::UsersAndGroups::Group;
use EBox::UsersAndGroups;
EBox::init();
open (my $USERS, 'new_users.txt');
while (my $line = <$USERS>) {
chomp ($line);
my $user;
my ($username, $givenname, $surname, $password, $groups) = split(',', $line);
$user->{'user'} = $username;
$user->{'givenname'} = $givenname;
$user->{'surname'} = $surname;
$user->{'password'} = $password;
print "\n Creating user: $username $givenname $surname \n";
my $newUser = EBox::UsersAndGroups::User->create($user, 0);
print "done\n";
foreach my $groupname (split(' ' , $groups)) {
print "Adding to Group $groupname \n";
my $group = new EBox::UsersAndGroups->group($groupname);
#print $group;
$newUser->addGroup($group)
}
}
close ($USERS);
1;
you'll need a file with the format
$username, $givenname, $surname, $password, $groups
where groups are SPACE seperated groups
have fun
-
Good idea, but I think, that " " is not a best separator for groups - better way is to use ";" - so your groups can be called for example "Mail users" or "Sale department".
-
Last script works, but needs groups, that are beeing assinged to user, to be created earlier.
I try to modify it adding:
my $group = new EBox::UsersAndGroups::Group->create($groupname);
but API returns:
Missing argument: entry|dn|ldifk
Any ideas how to deal with it?
-
hi guys,
any progress in this? im also looking for a way on how to do user and group bulk provisioning. I tried both scripts in this thread but no success.
The first script returns this error:
Creating user: useraccount firstname lastname
Can't locate object method "addUser" via package "EBox::Users" at groupprov1.pl line 23, <$USERS> line 1.
The second script returns this error:
Can't locate EBox/UsersAndGroups/User.pm in @INC (@INC contains: /etc/perl /usr/local/lib/perl/5.14.2 /usr/local/share/perl/5.14.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.14 /usr/share/perl/5.14 /usr/local/lib/site_perl .) at groupprov2.pl line 7.
BEGIN failed--compilation aborted at groupprov2.pl line 7.
Thanks in advance,
-
Hi
Could you please let me know how we can do it in the reverse I mean disable or delete users?
Thanks in advance!