Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Automatic useradd and password

Status
Not open for further replies.

mvvilleza

MIS
Nov 23, 2000
114
PH
Hi all,

I have a list of usernames saved in a file. Can anyone help me how I can read this file to auto-create the usernames and assign an auto-generated password where this password will be saved to a file?

Thanks for the help guys.
 
You could use perl or bash... some rough perl would look like this:
Code:
#!/usr/bin/perl -w
# I borrowed the following routine to generate a random string from the author credited
###########################################################
# Written by Guy Malachi [URL unfurl="true"]http://guymal.com[/URL]
# 18 August, 2002
###########################################################

# This function generates random strings of a given length
sub generate_random_string
{
	my $length_of_randomstring=shift;# the length of 
			 # the random string to generate

	my @chars=('a'..'z','A'..'Z','0'..'9','_');
	my $random_string;
	foreach (1..$length_of_randomstring) 
	{
		# rand @chars will generate a random 
		# number between 0 and scalar @chars
		$random_string.=$chars[rand @chars];
	}
	return $random_string;
}

open (FOUT,">passwordlist.txt");
# My code now.  Save the perl code as "massuseradd.pl"
# Call this perl program with
# perl massuseradd.pl < listofusernames.txt
while (<>) {
  print "Processing Account Name: $_\n";
  print "Creating Group: $_\n";
  exec ("groupadd $_");
  print "Creating User: $_\n";
  exec ("useradd -G $_ $_");
# Generate the random string
  my $random_string=&generate_random_string(11);
  print "Setting Password for user: $_\n";
  print "You need to copy/paste or enter the password: $random_string\n";
  exec ("passwd $_");
  print FOUT "$_ --- $random_string";
  print "Done with user: $_\n---\n";
}
close FOUT;

# NOTE: This uses untested code and EXTREMELY POOR SECURITY PRACTICES.
# It is very bad to write out a master text file of users and passwords
# It is also very bad practice for the admin to see/enter every new password.

Please, no flames on the security merit of this hack. I know it's dangerous stuff. Also, no flames from the bash crowd - I'm more comfortable in perl.

Hosting Solutions for Home or Business.
 
Thanks for the reply,

I already have a solution, downloaded passgen package and then created a script. Something like :
#!/bin/bash
for j in $(cat /root/userlist.txt); do
PASS=`/usr/local/bin/passgen`
echo $PASS >> password.txt
echo $PASS|passwd --stdin $j
done

Again thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top