LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Adding new users via Shell script (https://www.linuxquestions.org/questions/linux-general-1/adding-new-users-via-shell-script-286805/)

coolfrog 02-06-2005 12:57 AM

Adding new users via Shell script
 
Hey,
I need to create a shell script which adds about 10-20 users automatically with username of the form "scoecomp1","scoecomp2" so on and default password "abc". How do i go about doing this as when the "adduser" command is executed in shell script the shell waits for inputs like username,uid etc from user. How do i make the script fill in these inputs?

heema 02-06-2005 03:59 AM

u could use useradd instead

useradd <username> -d <homedir> -g <group> -s <dflt shell> -c <users name> -p <password>

and u could try this script but i havent tried it but atleast u could look at its code

http://www.linuxquestions.org/questi...useradd+script

Crashed_Again 02-06-2005 08:02 AM

Heres a little python script that should do the trick:

Code:

SEE CHANGES BELOW
Note that if you want to add 10 users the line 'while x < 3:' will have to be 'while x < 11'. The value must be one more then the number of people you want to add.

<EDIT>Also make sure all the 'useradd' switches are to your likeing. This script will create a home directory for each user. Not sure if thats what you want but you can easily edit things.</EDIT>

Tinkster 02-06-2005 05:20 PM

Also note that the password that the -p option expects
is already ENCRYPTED ... if you use -p abc the user
won't be able to log in at all ...
Code:

quoted from man useradd
      -p passwd
              The encrypted password, as  returned  by  crypt(3).
              The default is to disable the account.



Cheers,
Tink

Crashed_Again 02-07-2005 05:37 AM

Good eye Tinkster. This should work now(if you have crypt installed) but I haven't tested it.

Code:

#!/usr/bin/env python

import os, commands

x = 1

while x < 3:
        username = "scoecomp" + str(x)
        os.system("useradd " + username + " -m -d /home/" + username + " -g users -s /bin/bash -p " + commands.getoutput('crypt abc'))
        x += 1


coolfrog 02-15-2005 10:20 AM

Hey all,
Thanx for the replies. But it would be great if i could get this done without using Python.

jmercier 02-15-2005 12:04 PM

Bash Script.
 
Give this a try. Tested on bash.


Code:

:

userprefix=scoecomp
totalusers=10
clear_passwd='badpass'
crypt_passwd=`openssl passwd -crypt $clear_passwd`

for((i=1;$i<=${totalusers};i +=1))
 do
  useradd ${userprefix}${i} -d /home/${userprefix}${i} -g users -p $crypt_passwd
 done


Crashed_Again 02-15-2005 01:10 PM

Quote:

Originally posted by coolfrog
Hey all,
Thanx for the replies. But it would be great if i could get this done without using Python.

What? No python? boooooo ;)

caps_phisto 02-16-2005 02:24 PM

Give this one a try:

Code:

USER_ACCT="scoecomp"


useradd -d /home/scoecomp1 -m -s /bin/bash -p <encrypted password here> scoecomp1
PASSWORD=`grep '^'scoecomp1':' /etc/shadow | cut -d: -f2`

cp /etc/shadow /etc/shadow.bak

START=2
TOTAL=20

while [ $TOTAL -gt 0 ];
  do
    ACCT=${USER_ACCT}${START}
    START=`expr $START + 1`
    TOTAL=`expr $TOTAL - 1`
    useradd -g 10 -d /home/${ACCT} -m -s /bin/bash ${ACCT}
    RIGHT_SIDE=`grep ${ACCT} /etc/shadow | cut -d: -f3-`
    echo ${ACCT}:${PASSWORD}:${RIGHT_SIDE} >>  /etc/shadow.bak
    chown -R ${ACCT} /home/${ACCT}
  done

chown -R user1 /home/scoecomp1

cp -f /etc/shadow.bak /etc/shadow

All bash here! Have fun!

coolfrog 02-19-2005 06:14 AM

Thanx all.

irshadinweb 12-05-2010 10:47 AM

Creating users using script
 
for i in $(seq 1 20);do useradd scoecomp$i;echo "abc" | passwd scoecomp$i --stdin;done

Just copy n paste the above script in ur terminal and ur done !!


All times are GMT -5. The time now is 10:36 PM.