LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-02-2003, 08:02 AM   #1
Linx2003
LQ Newbie
 
Registered: Sep 2003
Location: NZ
Posts: 3

Rep: Reputation: 0
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.
 
Old 09-02-2003, 08:19 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,397

Rep: Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963Reputation: 1963
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
 
Old 09-02-2003, 08:25 AM   #3
Claus
Member
 
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74

Rep: Reputation: 15
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??
 
Old 09-02-2003, 09:05 AM   #4
zekko
Member
 
Registered: Aug 2003
Location: Canada
Distribution: Slackware, debian
Posts: 76

Rep: Reputation: 15
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
 
Old 09-02-2003, 09:51 AM   #5
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
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
 
Old 09-02-2003, 10:29 AM   #6
Claus
Member
 
Registered: Jul 2003
Location: Santiago de Chile
Distribution: Debian testing/unstable
Posts: 74

Rep: Reputation: 15
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
 
Old 09-02-2003, 11:04 AM   #7
SaTaN
Member
 
Registered: Aug 2003
Location: Suprisingly in Heaven
Posts: 223

Rep: Reputation: 32
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.
 
Old 09-02-2003, 01:00 PM   #8
abd_bela
Member
 
Registered: Dec 2002
Location: algeria
Distribution: redhat 7.3, debian lenny
Posts: 594

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
create new users in squirellmail bharath144 Linux - Software 1 11-26-2005 03:02 PM
Can't create users! Zeke55 Linux - Newbie 9 12-08-2004 01:23 PM
create users using script polis Linux - General 1 05-18-2004 08:19 AM
How to use bash shell to create many users Linx2003 Linux - Software 1 09-04-2003 11:14 PM
How to use bash shell to create many users Linx2003 Linux - General 1 09-02-2003 08:18 AM


All times are GMT -5. The time now is 10:54 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration