Re: Adding a lot of users in a single operation.
here is what I use to add users from the file.
On Thu, Dec 02, 1999 at 02:36:25PM -0600, Jens B. Jorgensen wrote:
> The trouble is that none of the utilities (that I know of) allow you to specify the
> password on the command line. Perhaps you can do it with pipes, like the following.
>
> Ok, let's say you have a file users.txt with a list of user names you want to add, one
> per line.
>
> for i in $(cat users.txt) ; do pwd=$(makepasswd) ; echo $i $pwd >> userpass.txt ;
> adduser --disabled-password --gecos $i $i ; cpwd=$(perl -e 'print crypt("'$pwd'",
> "'$i'");') ; echo -e '/^'$i':/ s/:!:/:'$cpwd':/\nw\n' | ed /etc/passwd ; done
>
> That should do it. (See, you had to user perl after all.) Mind you the above is all
> one line, you'll have to fix the breaks inserted by my mail-reader and yours. Good
> practice dictates you save your /etc/passwd file before completing the above. Also,
> the above assumes you are not using shadow passwords. In any case the above should be
> adaptable to shadow passwords too if you can see what I'm doing.
>
> Jose L Gomez Dans wrote:
>
> > Hi!
> > I was wondering whether it would be possible to add a number of
> > users with a single command. Ideally, there would be a call to pwgen (or
> > something like that!) and the passwords would be held in a file.
> >
> > Any ideas (apart from "do it in perl!")?
> > Jose
> > --
--
Marcin Kurc
Indiana Institute of Technology
System Administrator
http://me.indtech.edu http://www.indtech.edu
#!/usr/bin/perl -w
$firstuid=`tail --lines 1 /etc/passwd | cut -d : -f 3;`;
$firstuid+=1;
$gid=1003;
$src_file="users.txt";
$dest_file="users.pw_list";
print "Give me the UID you want to start with[$firstuid]:\n";
chop($_=<STDIN>);
$firstuid=$_ if $_;
print "Give me the GID[$gid]:\n";
chop($_=<STDIN>);
$gid=$_ if $_;
print "Give me the filename with the names[$src_file]:\n";
chop($_=<STDIN>);
$src_file=$_ if $_;
print "Give me the destination filename[$dest_file]:\n";
chop($_=<STDIN>);
$dest_file=$_ if $_;
print "Are these OK?\n";
print " First UID: $firstuid\n";
print " GID: $gid\n";
print "Source File: $src_file\n";
print " Dest File: $dest_file\n";
chop($_=<STDIN>);
die "Quitting\n" unless /^[yY]/;
$homeroot="/home";
$shell="/bin/bash";
open SRC, "<$src_file" or die "Can't open $src_file\n";
chop (@users=<SRC>);
close SRC;
$nruser=$#users+1;
@clpasses=`pwgen 8 $nruser`;
open SH, ">>/etc/shadow";
open PW, ">>/etc/passwd";
open DEST, ">$dest_file" or die "Can't open $dest_file\n";
for ($a=0;$a<$nruser;$a++)
{
$uname=$users[$a];
# $uname =~ s/^\s*(\w)\w+\s+(\w)\w+\s+(\w+)\s*$/$1$2$3/;
# $uname =~ s/^\s*(\w)\w+\s+(\w+)\s*$/$1$2/;
# $uname =~ s/^(.{1,8}).*/$1/;
# $uname = lc $uname;
($uname, $users[$a]) = split /\t/, $users[$a], 2;
$clpass=$clpasses[$a];
chop $clpass;
$crpass=crypt $clpass,$clpass;
print DEST "user: $uname ($users[$a]) pass: $clpass\n";
print SH "$uname:${crpass}:10693:0:99999:7:::\n";
print PW "$uname:x:$firstuid:$gid:$users[$a]:$homeroot/$uname:$shell\n";
mkdir ("/home/$uname", 0755);
chown ("$firstuid", "$gid", "/home/$uname");
$firstuid++;
}
close SH;
close PW;
close DEST;
Reply to: