Helpful Information
 
 
Category: Scripts
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?

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


#!/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.

Thanks alot. It works!

ermm by the way how do you compile/execute a ksh in linux terminal?

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.

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

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


#!/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

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.

thanks druuna :tntworth: :tntworth: :)

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



sudo smbpasswd -a username
New smb password : -------
retype new smb password: -----

Can you design a shell script for me to do this?? :confused: :confused:

Thanks

thanks druuna :tntworth: :tntworth: :)

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



sudo smbpasswd -a username
New smb password : -------
retype new smb password: -----

Can you design a shell script for me to do this?? :confused: :confused:

Thanks

Have you ever used expect? That's why I use to generate samba accounts.

bullet thanks

But no i never have used expect. Can you please give me some details. Also note i am using ubuntu 7.04.

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.


#!/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

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










privacy (GDPR)