This how-to is intended to import users to eBox from a CSV (comma separated file) in bulk.
- Put the following script in a text file called bulkusers and made it executable for root in root's home directory.
#!/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");
while(my $line = <$USERS>) {
chomp ($line);
my $user;
my ($username,$fullname,$password) = split(',',$line);
$user->{'user'} = $username;
$user->{'fullname'} = $fullname;
$user->{'password'} = $password;
push(@users,$user);
}
close($USERS);
foreach my $user (@users) {
$usersModule->addUser($user, 0);
}
1;
- Before running the script you need to have a file called users in the same directory. Inside the file it should look like the following:
jblow,Joe Blow,jblowspassword, jdoe,Jane Doe,jdoespassword,
Notes on this users file:
- no blank lines or the script blows up
- it may include a trailing comma after the password (otherwise the account may be created but the password isn't added). I exported this file from excel and thought that MSDOS CSV file type would be what to choose but trailing commas were not added. Apparently Windows CSV file type adds the trailing commas.
- before running the script open the file in vi or something and get rid of the trailing M's on the end of lines (:%s/M$//g is the command to do this in vi where M is created by pressing ctrl-v then ctrl-m)
Thanks to Jamie Reid and other testers
