Awk passwd

Discussion in 'Programming/Scripts' started by Nate1, Aug 31, 2006.

  1. Nate1

    Nate1 New Member

    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
     
  2. Nate1

    Nate1 New Member

    crypt

    Well i have also stumbled accross crypt 3 apparently an encryption function does anyone know how to use it?
     
  3. Nate1

    Nate1 New Member

    answer

    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.
     
  4. falko

    falko Super Moderator Howtoforge Staff

    Good to know. :)
     
  5. drks

    drks New Member HowtoForge Supporter

    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:

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

    Code:
    apt-get update && apt-get install libcrypt-passwdmd5-perl
    

    For any standard PERL install, you can also just use CPAN:

    Code:
    perl -MCPAN -e shell
    
    cpan> install Crypt::PasswdMD5
    

    Hope that is useful.
     
    Last edited: Sep 3, 2006

Share This Page