LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to use bash shell to create many users (https://www.linuxquestions.org/questions/programming-9/how-to-use-bash-shell-to-create-many-users-88370/)

Linx2003 09-02-2003 08:02 AM

How to use bash shell to create many users
 
How to use bash shell to create many users

Hi all, my name is Rick. I’m using red hat 9.0
Can somebody show me how to add many accounts by a bash script?

I need to perform the following functions:
1) I have a file called userfile.txt. It contains new users’ information. The format is like this:
Username:Password:groupname:fullname
Example
Mike:1234:IT:Mike White
John:5732:Accounting:John Smith
so on ...


2) I now need to create a shell script called:myscript , this script program will read the userfile.txt file and process the user account.
1.To use this program, user must type this command: “myscript userfile.txt”. If user does not type in this way, system will display the error message—“you type in wrong way !”
2.If people type in myscript –h, system will display some information to the users.

acid_kewpie 09-02-2003 08:19 AM

Please do not post the same thread in more than one forum. Picking the most relevant forum and posting it once there makes it easier for other members to help you and keeps the discussion all in one place.

http://www.linuxquestions.org/rules.php

Claus 09-02-2003 08:25 AM

it sounds like thats your homework boy!!!!!!!!!

anyway, sometime ago i needed to create aprox 40 acounts.... and i didnt know how to put the passwords in the 'useradd' command, by a script.....

who knows??

zekko 09-02-2003 09:05 AM

This is a Perl script, so if you have Perl installed on your system it should work:

Quote:

#!/usr/bin/perl
die "you type in wrong way!\n" if (!defined $ARGV[0]);

if ($ARGV[0] eq '-h') {
print "Useful help information goes here!\n";
exit(0);
}

while (<>) {
chomp;
my ($username, $password, $groupname, @fullname) = split(':', $_);
print "Creating new user:\n";
print "Username: $username\nPassword: $password\nGroupname: $groupname\nFullname: @fullname\n\n\n";
# system("adduser $username $password $groupname @fullname");
}
One thing to note, I dont know how to add users through the command line, so you'll have to change that. I did put that system function in a comment, you can just edit it to the right format.

Usage: perl myscript.pl <-h|userlist.txt>

Sorry this script is so messy :(

SaTaN 09-02-2003 09:51 AM

Quote:

Originally posted by Claus
it sounds like thats your homework boy!!!!!!!!!

anyway, sometime ago i needed to create aprox 40 acounts.... and i didnt know how to put the passwords in the 'useradd' command, by a script.....

who knows??


Maybe u didn't check the man pages for useradd

There is an option -p in useradd can be used ....

Also check "man passwd", there is an option --stdin
I suppose , Even this can be used...

Welcome to LQ

Claus 09-02-2003 10:29 AM

absolutely right man...

Linx2003, try to do it for your own.. all you need is to read the BASH Programing HOWTO.... you'll see that's pretty easy :P

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html

SaTaN 09-02-2003 11:04 AM

The previous post of mine was intended for you also Claus.

Quote:

Originally posted by Claus
i needed to create aprox 40 acounts.... and i didnt know how to put the passwords in the 'useradd' command, by a script.....

who knows??

I thought that my previous message would help you.

abd_bela 09-02-2003 01:00 PM

hi,
use the adduser with --stdin option to create a count with an uncrypted passwd.

after that you have to crypt the passwd. this a small program in C to crypt a passwd:
#include <unistd.h>
#include <stdio.h>

char *crypt(const char *key, const char *salt);

//char *crypt(char *key, char *salt);

int main(){
char passwd[20];
char key[]="mt"; // some couple of characters see man crypt
char *crypted;
crypted = new char [20];
printf ("\n enter password ");
scanf("%s", passwd);
crypted = crypt( passwd, key);
printf ("\nthe passwd before is %s after is %s ", passwd, crypted);
return(0);
}

to compile it, add the crypt library
g++ crypt1.c -lcrypt

best regards
bela


All times are GMT -5. The time now is 11:30 PM.