Sponsored Content
Full Discussion: useradd script
Top Forums UNIX for Dummies Questions & Answers useradd script Post 30556 by RTM on Wednesday 23rd of October 2002 05:20:52 PM
Old 10-23-2002
You can hack this if you want - no warrenties come with it of course.

1/2 half of code:

Code:
#!/u/bin/perl
#    This is a script to add users to the passwd / shadow files.  It
# prompts for a first name, middle inital and last name.  A username
# is generated from the first inital, middle inital, and lastname.
# This username can be changed.  After a username has been selected
# the passwd file is scaned to see if the user already exists.  If the
# username exists then the user is prompted to change the username.
#   Once a valid username is chosen, the passwd file is scaned to find
# the next free userid.  This userid must be greater than the $minuid
# variable.  The entry is added to the passwd and shadow files, the
# home direcrory is created and 'make passwd' is done.
#
# $Revision: 1.5 $

#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# These values are changable.  Set them to the configuration
#  of enviroment.

#This is the NIS domain name.
chomp ($domain = `/bin/domainname`);

# This file contains a log of the accounts and passwords for the 
# created users.  Look here for the passwords.
$PASSWD_FILE="/var/nis/bin/PASS.OUT";

# This is the shell to give the user
$shell="/bin/ksh";

# This is the location of the homedir.  This is where to make it.
# the homedir passwd entry is set to /home/$username
$homedir='/toast1/home/home/';

# The location of the source files.
$NISfiles="/var/nis/$domain/masters";

# This is the salt used when crypting the password
$SALT="qtzjk24s1";

# Command to make the passwd files
$MAKE = "cd /var/nis/$domain ; /u/bin/make passwd";

# location of the cvs (version control system) executable
$CVS = "/u/bin/cvs -n";

#default location of CVS
$CVSROOT = "/adm/cvs/cvsroot";

#First UID to start with
$FIRST_UID = 1500;



#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

###############################################################
# let the coding begin
###############################################################

BEGIN {
        print ("\n\nNIS account creation utility\n\n\n");
}

#Check and make sure everything is cool with CVS before we start
#&check_cvs if ( -f $CVS);


# reads passwd file and assigns it to PASS file handle.  
# takes each line in PASS filehandle and assigns each line to an individual
# element in array passwd.

open PASS, "<$NISfiles/passwd" or die "can't open $NISfiles/passwd";
while (<PASS>) {

        chomp;

        #skip comments and whitespace
        next if (/^\s*#/ or /^\s*$/);

        # get the fields in the passwd entry
        ($uname,$x,$uid,$gid,$gecos,$home,$shell)  = split ':', $_;

        # trying to be careful
        if ($uname eq "") { die "ERROR: bad line in passwd:\n$_\n\n" }

        # check for duplicate uids and unames
        if ( $passwd {$uid} ) {
                die "ERROR: duplicate entries for UID $uid \n$passwd{$uid} \n$_ 
\nPlease correct error before using adduser.\n\n";

        } elsif ($uname2uid {$uname}) {
                die "ERROR: duplicate entries for $uname \n $passwd{$uname2uid{$
uname}} \n $_ \nPlease correct this error before using adduser.\n\n";
        }

        # key the passwd list on uid. used to sort the file by uid
        $passwd {$uid} = $_;

        # store an indexed list of unames used to detect duplicate unames
        $uname2uid {$uname} = $uid;

}
close PASS;

open SHADOW, "<$NISfiles/shadow" or die "can't open shadow in $NISfiles";
while (<SHADOW>) {

        chomp;

        # skip comments and whitespace
        next if (/^\s*#/ or /^\s*$/);

        # get the fields in the shadow entry
        ($uname) = split /:/, $_;

        # die if no passwd entry exists for uname 
        unless ( $uname2uid {$uname}) 
                { die "ERROR: User $uname exists in shadow but not in passwd fil
e.\nPlease correct this error before using adduser.\n\n" }

        # key the shadow list on uid by converting uname to uid
        $uid = $uname2uid {$uname};

        # error if uid not in passwd file
        unless ($uid) {
            die "ERROR: user $uname in shadow but not in passwd.  This error may
 also be caused by having a valid passwd entry with uid equal to zero.  This pro
gram does not support uid = 0 since it was designed for use with NIS.\nPlease co
rrect this error.\n\n";
        }

        # check for duplicate shadow entries
        if ($shadow {$uid})
            { die "ERROR: duplicate shadow entry for $uname\n$shadow{$uid}\n$_\n
\n" }

        # finally store the shadow entry in a hash ordered by username.
        $shadow {$uname2uid {$uname}} = $_;
}
close SHADOW;

# check for users in passwd but not in shadow
for $uid (keys %passwd) {

    unless ($shadow {$uid}) 
        { die "This user is missing a shadow entry:\n$passwd{$uid}" }

}

#init rand number generator
srand(time|$$);

########################
### Begin Loop that allows mutiple accounts to be added at once
#########

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

useradd -c in a script????

Here's my box info: ROOT@fcivra: uname -a SunOS fcivra 5.6 Generic_105181-26 sun4m sparc SUNW,SPARCstation-5 I am creating a script called adduser, that will incorporate the useradd command. Here is what it looks like: #!/bin/sh echo "Enter the person's VZID (lower case): \c" read VZID... (2 Replies)
Discussion started by: cdunavent
2 Replies

2. Shell Programming and Scripting

useradd

Gurus, I need to add a user to all the machines. I need a script to do this. I did one but it does not allow me to su to root within a ssh session i open. It exists saying su: Sorry. Please let me know how i can do it. I do not have the freedom of using sudo either. Regards (4 Replies)
Discussion started by: earlysame55
4 Replies

3. UNIX for Advanced & Expert Users

useradd?

Hi Experts, when using useradd command, what are the necessary options/arguments to be included? Please advice. (4 Replies)
Discussion started by: etcpasswd
4 Replies

4. Shell Programming and Scripting

Help with useradd script

Ok Im trying too make this shell script create users from my text file, I also want to type in a password for the new users. So thay can make a uniq one themself after first logon. #!/bin/sh # Sebastian schmidt clear echo "*************************************************************"... (3 Replies)
Discussion started by: chipmunken
3 Replies

5. Shell Programming and Scripting

trouble making a useradd script

i'm new to scripting in unix and am trying to make a script to add a user and an encrypted password for them. this is what i have and it isn't giving me any errors, but when i try to login with the new user, the password doesn't work. i'm hoping someone can point me in the right direction ... (1 Reply)
Discussion started by: patt4179
1 Replies

6. Solaris

useradd

if useradd command is deleted in solaris how do we add user (3 Replies)
Discussion started by: vivek_ng
3 Replies

7. Shell Programming and Scripting

need script for smbldap-useradd/setquota/ldapadd

Hello guys, I am trying to add users to samba/ldap system, set their quota, set them a common password and add auto_home records for the new users User list file is in below format Firstname Lastname Username Command I run to add user is smbldap-useradd -a -m -N $Firstname -S... (0 Replies)
Discussion started by: upengan78
0 Replies

8. Solaris

useradd

I want to creat a 27 logins in solaris.Can anyone tell me how to write a script for that so that i create at a time for all 27 people. Thanks to guide me. (6 Replies)
Discussion started by: kkalyan
6 Replies

9. Shell Programming and Scripting

Useradd script

Trying to create a bash script that will read the /etc/passwd file for existing UID's before adding a new user and also does an incremental by allocating the next available UID.And also add the user to the correct group.Please help I am a newbie in scripting. (2 Replies)
Discussion started by: tshepang
2 Replies
All times are GMT -4. The time now is 07:25 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy