The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
useradd earlysame55 Shell Programming and Scripting 4 08-28-2007 12:37 PM
[ksh] useradd chodaboy UNIX for Dummies Questions & Answers 3 06-30-2006 09:24 AM
useradd -c in a script???? cdunavent Shell Programming and Scripting 2 01-28-2005 12:36 PM
useradd dorilevy UNIX for Advanced & Expert Users 2 10-19-2004 02:15 AM
useradd ortsvorsteher UNIX for Dummies Questions & Answers 6 05-30-2002 03:28 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 10-22-2002
macdonto macdonto is offline
Registered User
  
 

Join Date: Jul 2001
Location: Corvallis
Posts: 61
useradd script

I have a txt file with many users and I need to add them and create random passwords. Does any one have a script in pearl possible?
  #2 (permalink)  
Old 10-23-2002
Kelam_Magnus's Avatar
Kelam_Magnus Kelam_Magnus is offline Forum Advisor  
Registered User
  
 

Join Date: Aug 2001
Location: DFW McKinney, TX,
Posts: 1,069
I don't know of any off the bat, but there are many great shell scripting sites on the internet.

It is fairly easy to do this with 1 password or NO password for all, but random passwords requires more work.

You can use the command line version of your useradd or adduser program to add them all to your system, but you will have to script something for the password creation.

  #3 (permalink)  
Old 10-23-2002
RTM's Avatar
RTM RTM is offline Forum Advisor  
Hog Hunter
  
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
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
#########
  #4 (permalink)  
Old 10-23-2002
RTM's Avatar
RTM RTM is offline Forum Advisor  
Hog Hunter
  
 

Join Date: Apr 2002
Location: On my motorcycle
Posts: 3,039
2nd half (too many characters for one post)

Code:

do {


#prompt administrator for user information

&read_new_user();

# Ask if name is correct and check username. If mk_unique_user results
# False then root decided not to create this user. 

 if(&mk_unique_user) {

    # Pick a initial password.

    $rvalue = int(rand(1000));
    ($uhead) = ( $name[0] =~ /^(.{1,4})/ );
    $nv = join('',$uhead,$rvalue);
    $pwd = crypt($nv,$SALT);
    
    # scan the passwd list for the first unused uid
    for ($uid = $FIRST_UID; $passwd{$uid}; ++$uid) {} 

    # choose group for new user
    &choose_group();

    # add user info to passwd map file
 
    $passwd {$uid} ="$username:x:$uid:$gid:@name:/home/$username:$shell";
    $uname2uid {$username} = $uid;

    # add user info to the shadow map file
    $shadow {$uid} = $username . ":" . $pwd . ":::::::";
 
    # save the new passwd file
    &save_pass;

    # create home dir
    &build_dir;

    # save the new passwd file
    &output_info;

  }

 # Ask if more users are to be added

  do {
    print "\nAdd another User (y or n)?";
    chop($confirm = <STDIN>);
    $confirm =~ tr/YN/yn/;
  } while (($confirm ne "y" )&&( $confirm ne "n") &&( $confirm ne ""));
} while ( $confirm eq "y");

### End of loop for users
 

### Make the NIS database
&build_database;

#end of main -------------------------


#------------------------------------------------------------------ 
# BUILD_DIR: Create the directory for the new user
sub build_dir {
    my $userhome;
    print "Creating user's home directory\n";
    $userhome = join '', $homedir, $username;
    system ("mkdir", $userhome);
    system ("cp /etc/skel/local.profile $userhome/.profile");
    system ("chown -R $uid:$gid $userhome");
    print "\n";
}

#------------------------------------------------------------------
# MK_UNIQUE_USER: Search the passwd database for user
#                 prompts for new username if name is not unique
sub mk_unique_user {
  my ($i,$confirm, $puser, $unique);
  my ($line); #line of passwd

  #confirm username
  do {
    print "Allow \"@name\" as \"$username\"  (y or n)";
    chop($confirm = <STDIN>);
  } while (($confirm ne "y" )&&( $confirm ne "n") &&( $confirm ne ""));
  if (($confirm eq "n") || ($username eq "")) {
    do {
      do {
         print "(s)kip this entry or (c)hange username: ";
         chop($confirm = <STDIN>);
      } while (($confirm ne "s" )&&( $confirm ne "c"));
      if ($confirm eq "s") 
         { return 0; } #skip this user
       print "Enter new username: ";
       chop($username = <STDIN>);
       print "Confirm \"@name\" as \"$username\" (y or n)";
       chop($confirm = <STDIN>);
     } while (($username ne "")  && ($confirm ne 'y') && ($confirm ne ""));
  }

  #find duplicate usernames 
  # continue looping until $username is not in the uname2uid list
  while ($uname2uid {$username}) {

        # print the conflict
        print "$username requested for @name but found in:\n$passwd{$uname2uid {
$username}}\n";
        # ask if we should not create an account or change the username
        do {
          print "(s)kip account creation or (c)hange username: ";
          chop($confirm = <STDIN>);
        } while (($confirm ne "s" )&&( $confirm ne "c"));


        # skip this account and exit the function
        return 0 if ($confirm eq "s");

        do {

           #get a new username
           print "Enter new username: ";
           chop($username = <STDIN>);

           #confirm the new name
           print "Confirm \"@name\" as \"$username\"  (y or n)";
           chop($confirm = <STDIN>);

        } while (($confirm ne 'y') && ($confirm ne ""));

  } 

  return 1; #do this user
}


#------------------------------------------------------------------ 
# BUILD_DATABASE: Build yp maps
sub build_database 
{
  print "Building password database... \n";
  system( $MAKE ) == 0 
    or die "failed to Make password database";
}

#------------------------------------------------------------------ 
# OUTPUT_INFO: Output new passwd information
sub output_info 
{
    # make a backup copy of the passwd file
    rename("$NISfiles/passwd", "$NISfiles/passwd.old") || die "can't move passwd
";

    # write the new passwd file
    open PASSWD,">$NISfiles/passwd" or die "ERROR: can't write new passwd: $!\n"
;
    for (sort keys %passwd) 
        { print PASSWD $passwd{$_}, "\n" }
    close PASSWD;
        
    # make a backup copy of the shadow file
    rename("$NISfiles/shadow", "$NISfiles/shadow.old") || die "can't move $NISfi
les/shadow to $NISfiles/shadow.old: $!";

    # write the new shadow file
    open(SHADOW,">$NISfiles/shadow") or die "ERROR: can't write new shadow: $!\n
";
    for (sort keys %shadow) 
        { print SHADOW $shadow{$_}, "\n" }
    close SHADOW;       
}


# This saves the username and password from the newly created account
#--------------------------------------------------------------------
sub save_pass 
{
  open(OUTPUT,">>$PASSWD_FILE");
  print OUTPUT "*************************** ";
  print OUTPUT (scalar localtime),"\n\n";
  printf(OUTPUT " NAME: %20s %20s %20s \n\n",$name[0],$name[1],$name[2]);
  printf(OUTPUT " Your username is %15s \n",$username);
  printf(OUTPUT " Your password is %15s \n\n",$nv);
  print OUTPUT "After you login change your password with passwd \n\n";
  print OUTPUT "*************************************************************\n"
;
  close(OUTPUT);
}


#--------------------------------------------------------------------
# read_user_name -- read the user info from the keyboard
#--------------------------------------------------------------------
sub read_new_user
{
        print "Enter user's first name: ";
        $name[0] = <STDIN>;
        chop ($name[0]);
        print "\nEnter user's middle initial: ";
        $name[1] = <STDIN>;
        chop ($name[1]);
        print "\nEnter user's last name: ";
        $name[2] = <STDIN>;
        chop ($name[2]);
        print "\n";

        ($firstinital) = ($name[0] =~ /^(.)/);
        ($middleinital) = ($name[1] =~ /^(.)/);
        $lastname = $name[2] ;

        #create a username from first inital, middle inital, and last name
        $username = join ("",$firstinital,$middleinital,$lastname);

        # change username to lowercase
        $username =~ tr/A-Z/a-z/;

}




#--------------------------------------------------------------------
# choose_group -- prompt for the user's group membership
#--------------------------------------------------------------------
sub choose_group
{
    # by using list context, we read the group file quickly
    open GROUP, "<$NISfiles/group" or die "can't open group file in $NISfiles"; 
    @good_group = <GROUP>;
    close GROUP;
    print "-------------------------------------------\n"; 
    for $group (@good_group) {
        @line = split ':', $group;
        print $line[2], "\t", $line[0], "\n";
    }
    print "-------------------------------------------\n";
    print "what group should the user be in\n";
    $gid = <STDIN>;
    chop ($gid);
}


#--------------------------------------------------------------------
# Checks CVS to see if the files are up to date and etc.
# not in use yet -- cbmasters
#--------------------------------------------------------------------
sub save_pass_cvs 
{
    my $quit = 0;
    my ($USER, %CVS_stat);

    # check if the USER or LOGNAME enviromental variable is set
    # if they are not set then set them.  This works since make and cvs
    # will inherit this enviroment when they are forked later

    unless ($ENV{LOGNAME} or $ENV{USER}) {
        print <<EOF
For CVS to properly update, you must set LOGNAME or USER
to your username.  Please enter you login name now.
USER=EOF

    # read the username from stdin
    $USER = <STDIN>;

    # set the variables
    $ENV{USER} = $USER; $ENV{LOGNAME} = $USER;

    # check for CVSROOT and it is NOT defined then 
    # set it to the default from $CVSROOT
    if ($ENV{CVSROOT})
        { $CVSROOT = $ENV{CVSROOT} }
    else 
        {
        $ENV{CVSROOT}= $CVSROOT;
        print "WARNING: CVSROOT was not defined, so I set it to $CVSROOT\n";
    }

    # The next set of steps check to see if the files under CVS control
    # are up to date.  

    # run a status on the nis files
    open CVS, "$CVS -Q status $NISfiles/passwd $NISfiles/shadow" or die "can't d
o $CVS status $NISfiles/passwd $NISfiles/shadow because $!";

    while (<CVS>) {
        #skip lines not about status
        next unless /^File:/;

        #save the Status of the passwd & shadow
        $CVS_stat{passwd} = $1 if /passwd\s*Status:\s*(\S+)/;
        $CVS_stat{shadow} = $1 if /shadow\s*Status:\s*(\S+)/;

    }
    close CVS;

    #check if status was detected
    unless ($CVS_stat{shadow} and  $CVS_stat{passwd}) {
        print "$CVS -Q status did not return status for passwd and shadow";
        exit -1;
    }

    # let the user know the status
    print "$NISfiles/passwd is $CVS_stat{passwd}\n$NISfiles/shadow is $CVS_stat{
shadow}\n";

    #if the files are not up to do the update them

    if ($CVS_stat{passwd} ne "Up-to-date" or ($CVS_stat{shadow} ne "Up-to-date")
 {
    print <<EOF
One of the files was not up to date, please run the following command

#$CVS update $CVS_stat{passwd} $NISfiles/shadow

You might need this:

#USER=$USER; export $USER; CVSROOT = $CVSROOT; export $CVSROOT
EOF
        
    }

}
## end of file ##
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 12:01 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0