LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 02-06-2005, 12:57 AM   #1
coolfrog
Member
 
Registered: Sep 2004
Location: India
Distribution: Slackware 10.0
Posts: 77

Rep: Reputation: 15
Question 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?
 
Old 02-06-2005, 03:59 AM   #2
heema
Senior Member
 
Registered: Sep 2003
Location: Egypt
Distribution: Arch
Posts: 1,528

Rep: Reputation: 47
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
 
Old 02-06-2005, 08:02 AM   #3
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
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>

Last edited by Crashed_Again; 02-07-2005 at 08:26 AM.
 
Old 02-06-2005, 05:20 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
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
 
Old 02-07-2005, 05:37 AM   #5
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
Smile

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

Last edited by Crashed_Again; 02-15-2005 at 01:54 PM.
 
Old 02-15-2005, 10:20 AM   #6
coolfrog
Member
 
Registered: Sep 2004
Location: India
Distribution: Slackware 10.0
Posts: 77

Original Poster
Rep: Reputation: 15
Hey all,
Thanx for the replies. But it would be great if i could get this done without using Python.
 
Old 02-15-2005, 12:04 PM   #7
jmercier
LQ Newbie
 
Registered: Nov 2004
Location: Dallas, TX
Distribution: Suse 10
Posts: 25

Rep: Reputation: 15
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
 
Old 02-15-2005, 01:10 PM   #8
Crashed_Again
Senior Member
 
Registered: Dec 2002
Location: Atlantic City, NJ
Distribution: Ubuntu & Arch
Posts: 3,503

Rep: Reputation: 57
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
 
Old 02-16-2005, 02:24 PM   #9
caps_phisto
Member
 
Registered: Sep 2004
Location: NH
Distribution: FC6, FC1-4, RH9, Gentoo 2006.0/1, Slackware 10.1/2,11, Vector SOHO 5.0.1
Posts: 237

Rep: Reputation: 30
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!

Last edited by caps_phisto; 02-16-2005 at 02:25 PM.
 
Old 02-19-2005, 06:14 AM   #10
coolfrog
Member
 
Registered: Sep 2004
Location: India
Distribution: Slackware 10.0
Posts: 77

Original Poster
Rep: Reputation: 15
Thanx all.
 
Old 12-05-2010, 10:47 AM   #11
irshadinweb
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
Smile 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 !!

Last edited by irshadinweb; 12-05-2010 at 10:48 AM.
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script adding autostart gnome script Coolrunr Programming 3 01-01-2009 02:23 PM
Adding a shell script to startup Dakkar SUSE / openSUSE 3 10-15-2005 08:00 AM
Adding multiple user shell script plexus Programming 2 06-19-2004 08:36 PM
adding a job to crontab via shell script tazio Linux - General 7 10-23-2003 02:44 PM
Script that would automate adding users zyft02 Linux - Newbie 4 02-25-2002 11:19 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:53 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration