Re: adding multiple users, name format?
On Fri, Jun 17, 2005 at 12:23:32PM -0700, michael@etalon.net wrote:
>
> I'm in the process of adding thousands of users to our system.
> Our users have a short life span on the system and we get many new
> users every year.
> We have a small script that will add many users to our system.
> We can get a list of users and their passwords in this format:
>
> FirstName LastName Password
>
> From this file, we can script it down to another file for batch adding.
> Example user: John Smith pass123
> jsmith pass123
>
> From the format above, we can easily run a script and add our users.
> Our biggest problem occurs when we have duplicate names.
I'd put all existing users in a hash table/dictionary. This makes it
trivial to lookup whether some new user already exists.
The following little Perl script illustrates the idea:
#!/usr/bin/perl
my $old = $ARGV[0];
while (<>) {
my ($name, $pass) = split ' '; # parse input line
$name = lc $name; # treat name case-insensitive
if ($ARGV ne $old) { # from second file, i.e. new user?
while (exists $lookup{$name}) { # name already used?
# find other name
$name =~ s/(\d*)$/$1+1/e;
$name =~ s/\D(\d+)$/$1/ if length $name > 8;
}
# call script to add user:
system "./yourscript", $name, $pass;
}
$lookup{$name} = 1; # put in lookup table
}
In case a new user is found to exist, some other username is created
automatically, by adding/incrementing a number at the end of the name.
You can modify that algorithm to suit your needs, of course...
The script takes two arguments:
$ ./check_add_users oldusers newusers
The first arg is a file containing all existing users -- the first
value on the each line (whitespace-seperated) is taken to be the
username (any other stuff on the line is ignored, if present).
The second file contains all new users to add -- two values per line,
i.e. username password.
Replace "./yourscript" above with the user-add-script you already have.
It will be passed the username and password (not too surprisingly...:)
The script is not well-tested, so use at your own risk. A dry run
followed by a uniqeness check should suffice, though.
In case you want to modify something, but Perl isn't your native
language, feel free to mail me off-list.
Cheers,
Almut
Reply to: