View previous topic :: View next topic |
Author |
Message |
Messiah Tux's lil' helper
Joined: 30 Apr 2002 Posts: 139
|
Posted: Sun Jun 30, 2002 11:58 pm Post subject: another adduser script..this one adds users with dots & |
|
|
I wrote this script (in perl) because I needed to add users with a dot in their username. Also, I always use separate groups per user, so there is a group created with the same name as the username. Even quotas are supported.
This script will add users angroups beginning at uid 2000.
Below are the headers of the script.
Code: |
#!/usr/bin/perl
########################################################################
# Qweb Internet Services | info@qweb.nl #
########################################################################
# Please feel free to use, modify and distribute this script freely. #
# Please do NOT remove this header!! #
########################################################################
# If you make any changes in this script, please mail a copy with #
# comments to the e-mailaddress mentioned above. #
########################################################################
# adduser #
########################################################################
# This script will let you add a user on a Gentoo Linux system (or #
# any other system that uses /etc/passwd, /etc/shadow and /etc/group) #
# with a dot (.) in the username. It will also make a group with the #
# same name as the username, make a home-dir for the user, and copy #
# a skeleton directory. #
########################################################################
# v0.1 - Initial version #
########################################################################
# First, we have some global variables
#
# The skeleton directory to use
# /etc/skel is often used as a standard
$skeldir = "/etc/skel";
# Master home dir containing the home-directories of all users
# /home is a good one and is standard
$masterhome = "/home";
# Which shell?
$shell = "/bin/bash";
# Use quotas? This will need you to have quotatool
# (http://devel.duluoz.net/quotatool/) installed!
# Must be the path to quotatool, or it won´t be used!
$quotatool = "/usr/local/bin/quotatool";
# On what partition we have quotas?
$quotafilesys = "/";
|
[edit]
ermm...no option to include an attachment, so I will provide a link:
http://mustang.qweb.nl/scripts/adduser |
|
Back to top |
|
|
AutoBot l33t
Joined: 22 Apr 2002 Posts: 968 Location: Usually Out
|
Posted: Tue Jul 02, 2002 7:49 am Post subject: |
|
|
You could just include the perl script in [code]. _________________ This message self destructed a long time ago. |
|
Back to top |
|
|
Messiah Tux's lil' helper
Joined: 30 Apr 2002 Posts: 139
|
Posted: Thu Jul 04, 2002 9:43 pm Post subject: |
|
|
I even start to love my own script even more. It is even possible to add users with usernames beginning with a number. With the traditional adduse and useradd scripts found on many distributions one cannot do that too.
Actually, this script has helped myself more than I ever could think of. I can't imagine adding users all day long by hand in the config files |
|
Back to top |
|
|
Black666 n00b
Joined: 15 Jul 2002 Posts: 61 Location: Vienna (Austria)
|
Posted: Wed Jul 24, 2002 9:03 am Post subject: |
|
|
Thnx, looked for that one!!
Just one quick question: Is it necessary to have a group with the same name as the user? I've looked through my /etc/group and didn't find any groups matching my username.. |
|
Back to top |
|
|
Black666 n00b
Joined: 15 Jul 2002 Posts: 61 Location: Vienna (Austria)
|
Posted: Wed Jul 24, 2002 9:21 am Post subject: |
|
|
Tried your script...works fine.
But what would I need to change if I want my users to be in the group "users" and not in the group "john.smith" for example ... |
|
Back to top |
|
|
Black666 n00b
Joined: 15 Jul 2002 Posts: 61 Location: Vienna (Austria)
|
Posted: Wed Jul 24, 2002 9:45 am Post subject: |
|
|
Got it working.
I only have some basic bash scripting skills but if you read the perl script often enough you'll be able to understand what it does |
|
Back to top |
|
|
Naan Yaar Bodhisattva
Joined: 27 Jun 2002 Posts: 1549
|
Posted: Wed Jul 24, 2002 5:38 pm Post subject: |
|
|
Redhat does this. Gentoo does not. It is not going to be a problem either way under Gentoo.
Black666 wrote: | Thnx, looked for that one!!
Just one quick question: Is it necessary to have a group with the same name as the user? I've looked through my /etc/group and didn't find any groups matching my username.. |
|
|
Back to top |
|
|
atom Tux's lil' helper
Joined: 25 Nov 2002 Posts: 122 Location: Columbus, Ohio, USA
|
Posted: Tue Apr 13, 2004 9:18 pm Post subject: |
|
|
well, since nobody else has added it to this thread i figured i will before the script disappears from the site.
Code: | #!/usr/bin/perl
########################################################################
# Qweb Internet Services | info@qweb.nl #
########################################################################
# Please feel free to use, modify and distribute this script freely. #
# Please do NOT remove this header!! #
########################################################################
# If you make any changes in this script, please mail a copy with #
# comments to the e-mailaddress mentioned above. #
########################################################################
# adduser #
########################################################################
# This script will let you add a user on a Gentoo Linux system (or #
# any other system that uses /etc/passwd, /etc/shadow and /etc/group) #
# with a dot (.) in the username. It will also make a group with the #
# same name as the username, make a home-dir for the user, and copy #
# a skeleton directory. #
########################################################################
# v0.1 - Initial version #
########################################################################
# First, we have some global variables
#
# The skeleton directory to use
# /etc/skel is often used as a standard
$skeldir = "/etc/skel";
# Master home dir containing the home-directories of all users
# /home is a good one and is standard
$masterhome = "/home";
# Which shell?
$shell = "/bin/bash";
# Use quotas? This will need you to have quotatool
# (http://devel.duluoz.net/quotatool/) installed!
# Must be the path to quotatool, or it won´t be used!
$quotatool = "/usr/local/bin/quotatool";
# On what partition we have quotas?
$quotafilesys = "/";
unless (@ARGV == 2 || @ARGV == 3) {
die <<EOF
Usage: $0 username password (#size)
Where username is the user you want to add and
password is the password for the user and
optional #size is limit of quota
EOF
;
}
$username=$ARGV[0];
$password=$ARGV[1];
if ($ARGV[2]) { $size=$ARGV[2]; }
$uidtouse = uidtouse();
$gidtouse = gidtouse();
open(D,">>/etc/group") || die "Couldn't open the group file, $!\n";
print D "$username:x:$gidtouse:\n";
close(D) || die "$0: Couldn't close the group file, $!\n";
open(D,">>/etc/passwd") || die "Couldn't open the passwd file, $!\n";
print D "$username:x:$uidtouse:$gidtouse:\"$username\":$masterhome/$username:$shell\n";
close(D) || die "$0: Couldn't close the passwd file, $!\n";
open(D,">>/etc/shadow") || die "Couldn't open the shadow file, $!\n";
print D "$username:!:11868:0:99999:7:::\n";
close(D) || die "$0: Couldn't close the shadow file, $!\n";
$junk=`echo $username:$password |chpasswd`;
$junk = `mkdir $masterhome/$username`;
$junk = `mkdir $masterhome/$username/tmp`;
copyskeldir();
$junk = `chown -R $username:$username $masterhome/$username`;
$junk=`chmod -R 755 $masterhome/$username`;
$junk=`chmod 750 $masterhome/$username`;
$junk=`chmod 700 $masterhome/$username/tmp`;
if ($size>0 && -e "$quotatool")
{
$cmd = "$quotatool -u $uidtouse -bq $size"."M -l $size"."M $quotafilesys";
$junk = `$cmd`;
$cmd = "$quotatool -g $gidtouse -bq $size"."M -l $size"."M $quotafilesys";
$junk = `$cmd`;
}
sub uidtouse {
# This function will get the last uid that was used out of /etc/passwd
my $uid;
my %hash;
while ( ( undef, undef, $uid ) = getpwent() ) {
if ( $uid >= 2000 && $uid <= 3000 ) {
$hash{ $uid } = 1;
}
}
my @keys = sort {$a <=> $b} keys %hash;
for ( $keys[0] .. $keys[-1] + 1 ) {
unless ( exists $hash{ $_ } ) {
$z = $_;
if ($z eq "1" || $z eq "0") { $z = "2000"; }
return $z;
last;
}
}
}
sub gidtouse {
# This function will get the last uid that was used out of /etc/passwd
my $gid;
my %hash2;
while ( ( undef, undef, $gid ) = getgrent() ) {
if ( $gid >= 2000 && $gid <= 3000 ) {
$hash2{ $gid } = 1;
}
}
my @keys2 = sort {$a <=> $b} keys %hash2;
for ( $keys2[0] .. $keys2[-1] + 1 ) {
unless ( exists $hash2{ $_ } ) {
$y = $_;
if ($y eq "1" || $y eq "0") { $y = "2000"; }
return $y;
last;
}
}
}
sub copyskeldir()
{
opendir(DIR,$skeldir);
@dir = readdir(DIR);
closedir(DIR);
for (my $i = 2; $i < @dir; $i++) {
if (-f $skeldir."/".$dir[$i]) { $junk = `cp -f $skeldir/$dir[$i] $masterhome/$username/`; }
else { `cp -Rf $skeldir/$dir[$i] $masterhome/$username/`; }
}
} |
|
|
Back to top |
|
|
|