View Full Version : Awk passwd
Nate1
31st August 2006, 03:13
Hello Im trying to write a script to add users to the system
I have an awk script creating the users and editing the /etc/shadow file bu after I have added the users I want to insert there passwords from a file.
$1 username
$2 password
gawk -F: '{
system("passwd "$1)
then somehow enter the password twice to confirm it without user intervention
print $2
print $2
this will prompt for input for every user and then print the users password twice
}' inputfile
print doesn't work neither do a number of ways of using system, but there must be a way to do it can anyone help please
Nate1
1st September 2006, 01:00
Well i have also stumbled accross crypt 3 apparently an encryption function does anyone know how to use it?
Nate1
1st September 2006, 02:27
Well I figured it out
system("passwd --stdin "$1" < "filename")
this will force the password command to take the data from the file and give it to the user $1, and you can pipe the command to input the appropriate record.
falko
1st September 2006, 17:03
system("passwd --stdin "$1" < "filename")
Good to know. :)
drks
3rd September 2006, 20:58
I don't know about the whole using a file business, but this can just as easily be done using usermod after you create the user. You will of course have to have a way to crypt the password... for that reason, I use perl and Crypt::PasswdMD5 which might suit someone else's needs better. Reference the following:
#!/usr/bin/perl -w
use strict;
use Crypt::PasswdMD5;
my $username = "johnny";
my $clearPasswd = "johnnypass";
my $cryptPasswd = "";
my $salt = rand(99);
$cryptPasswd = unix_md5_crypt($clearPasswd, $salt);
system("useradd -m -d /home/$username $username");
system("usermod -p '$cryptPasswd' $username");
print "Username is $username, using password hash $cryptPasswd\n\n";
You would of course want to make a real script that allows you to dynamically read in the usernames, loop through them, etc... but this is how to use Perl to generate the md5 hash password, and usermod to set it dynamically.
Note that Crypt::PasswdMD5 is not generally standard for Perl installs. For Debian, you should be able to apt-get this:
apt-get update && apt-get install libcrypt-passwdmd5-perl
For any standard PERL install, you can also just use CPAN:
perl -MCPAN -e shell
cpan> install Crypt::PasswdMD5
Hope that is useful.
vBulletin® v3.7.4, Copyright ©2000-2009, Jelsoft Enterprises Ltd.