The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Web Site Management
> Scripts
|
Shell Script to create user in Linux using text file data
Discuss Shell Script to create user in Linux using text file data in the Scripts forum on Dev Shed. Shell Script to create user in Linux using text file data Scripts forum discussing topics including building, optimization, and implementation. Discuss which scripting language is best suited to your needs.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 4th, 2003, 01:17 AM
|
|
Contributing User
|
|
Join Date: Sep 2003
Location: The Little Red Dot
Posts: 149
Time spent in forums: 1 h 23 m 6 sec
Reputation Power: 10
|
|
|
Shell Script to create user in Linux using text file data
Shell Script to create user in Linux using text file data.
Is it possible?
|

September 5th, 2003, 06:36 AM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 137
Time spent in forums: 5 m 37 sec
Reputation Power: 0
|
|
Yep it is.
Here's a quick and dirty example.
You did not tell which info is in the text file and it's layout, so i'm goin to assume the following:
- 1 line per newuser,
- username password group are the space seperated fields
Code:
#!/usr/bin/ksh
NEW_USERS="/path/to/text_data_file"
HOME_BASE="/home/"
cat ${NEW_USERS} | \
while read USER PASSWORD GROUP
do
useradd -g ${GROUP} -p ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
done
From here you should be able to expand/add/subtract things you want/don't want.
Last edited by druuna : September 5th, 2003 at 11:07 AM.
|

September 5th, 2003, 10:33 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Location: The Little Red Dot
Posts: 149
Time spent in forums: 1 h 23 m 6 sec
Reputation Power: 10
|
|
|
Thanks alot. It works!
|

September 7th, 2003, 08:25 PM
|
|
Contributing User
|
|
Join Date: Sep 2003
Location: The Little Red Dot
Posts: 149
Time spent in forums: 1 h 23 m 6 sec
Reputation Power: 10
|
|
|
ermm by the way how do you compile/execute a ksh in linux terminal?
|

September 8th, 2003, 06:51 AM
|
|
Contributing User
|
|
Join Date: Sep 2003
Posts: 137
Time spent in forums: 5 m 37 sec
Reputation Power: 0
|
|
|
The same way you execute sh/bash:
scriptname.ksh <enter> (if found in PATH)
or
./scriptname.ksh <enter>
It could be that ksh (pdksh) is not installed by default (Suse doesn't, don't know about RedHat).
It should reside in /usr/bin or /bin. /bin/ksh should be linked to /usr/bin/ksh ( /bin/ksh -> ../usr/bin/ksh ).
But you don't need to worry if ksh isn't installed, bash is (almost) as good. Just change #!/usr/bin/ksh to #!/bin/sh or #!/bin/bash (sh should be linked to bash: /bin/sh -> bash.
Last edited by druuna : September 8th, 2003 at 06:55 AM.
|

October 12th, 2004, 03:59 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 1
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
Iam not able to login after creating a user using script
Hi, Everybody... Good Afternoon
Why I am not able to login to create a user .... by using the following script..
#!/usr/bin/ksh
NEW_USERS="/path/to/text_data_file"
HOME_BASE="/home/"
cat ${NEW_USERS} | \
while read USER PASSWORD GROUP
do
useradd -g ${GROUP} -p ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
done
using this script i create users but i am not able to login why it was...
I am really appreciate if some body helps me resolving this issue....
Zameer Ahmed Syed
|

October 15th, 2004, 04:20 AM
|
|
Registered User
|
|
Join Date: Oct 2004
Posts: 2
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
Quote: | Originally Posted by druuna Yep it is.
Here's a quick and dirty example.
You did not tell which info is in the text file and it's layout, so i'm goin to assume the following:
- 1 line per newuser,
- username password group are the space seperated fields
Code:
#!/usr/bin/ksh
NEW_USERS="/path/to/text_data_file"
HOME_BASE="/home/"
cat ${NEW_USERS} | \
while read USER PASSWORD GROUP
do
useradd -g ${GROUP} -p ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
done
From here you should be able to expand/add/subtract things you want/don't want. |
hey,
I am supposed to do a big project on unix shell script, but i dont really know wot to do, any1 can help me and give me some ideas about some projects involving shell script? e.g. internet analyzer using shell?
pliz contact me on nima_h_S@yahoo.com
regards
Nima
|

April 26th, 2007, 07:06 PM
|
|
Registered User
|
|
Join Date: Apr 2007
Posts: 1
Time spent in forums: 19 m 36 sec
Reputation Power: 0
|
|
|
I am using the above code to add users to a red hat Linux box; it works, but I want to add a name to the account useradd -c 'Test Test' but I am unable cause the space as the delimiter in the file that the script reads thinks a first and last name are two variables. Also, the password reset part the script does not work. The script will run but the password I set in the file does not work allow the user to log in. Any help would be great.
|

March 29th, 2008, 03:44 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Posts: 3
Time spent in forums: 37 m 4 sec
Reputation Power: 0
|
|
|
adding samba
thanks druuna
The script worked. Now what i need is to allow all these users to be able to access samba. To this i need to execute the following
for every single user that i created by using ur shell script
Code:
sudo smbpasswd -a username
New smb password : -------
retype new smb password: -----
Can you design a shell script for me to do this??
Thanks
|

March 31st, 2008, 10:42 AM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
Quote: | Originally Posted by quarkhirad thanks druuna
The script worked. Now what i need is to allow all these users to be able to access samba. To this i need to execute the following
for every single user that i created by using ur shell script
Code:
sudo smbpasswd -a username
New smb password : -------
retype new smb password: -----
Can you design a shell script for me to do this??
Thanks |
Have you ever used expect? That's why I use to generate samba accounts.
|

April 4th, 2008, 03:22 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Posts: 3
Time spent in forums: 37 m 4 sec
Reputation Power: 0
|
|
|
bullet thanks
But no i never have used expect. Can you please give me some details. Also note i am using ubuntu 7.04.
|

April 6th, 2008, 12:45 PM
|
 |
Java Junkie
|
|
Join Date: Jan 2004
Location: Mobile, Alabama
|
|
Quote: | Originally Posted by quarkhirad bullet thanks
But no i never have used expect. Can you please give me some details. Also note i am using ubuntu 7.04. |
This is a simple expect script for doing this.
Code:
#!/usr/bin/expect
spawn /usr/local/samba/bin/smbpasswd -a [lindex $argv 0]
expect "New*:" {send "[lindex $argv 1]\r"}
expect "Retype*:" {send "[lindex $argv 1]\r"}
expect "Password*" {break}
exit 0
Here, of course, I assume expect is in /usr/bin, and smbpasswd is in /usr/local/samba/bin
Assuming the script is named smbpasswd.exp, it would be called like this.
/path-to/smbpasswd.exp username password
|

September 27th, 2009, 05:28 AM
|
|
Registered User
|
|
Join Date: Sep 2009
Posts: 1
Time spent in forums: 20 m 39 sec
Reputation Power: 0
|
|
|
Hi Druuna, I am implementing the adduser script above buit it doesnt work out for me, i have this data file format:
leonard 123456
Bernard 123456
Elizabe 123456
Wakareg 123456
Thats' username and password
then the script i wrote is this:
#!/bin/bash
NEW_USERS="user_accounts"
HOME_BASE="/home/"
cat ${NEW_USERS} | \
while read USER PASSWORD
do
useradd -g ${PASSWORD} -m -d ${HOME_BASE}${USER} ${USER}
done
note: user_accounts is the name of the data file, and both the script and data file are in same directory. And the script name is user_script
then am executing like this ./user_script
when i press enter, this is the error i am getting:
[root@hoasnet-fe32dd00-34 Lab4]# ./user_script
useradd: unknown group 123456
useradd: unknown group 123456
useradd: unknown group 123456
useradd: unknown group 123456
useradd: invalid numeric argument '-m'
Please advice
Thanks
Lennie
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|