On Fri, 25 Feb 2011, Raymond Norton wrote:

> Got it done with the following script:
>
> #!/bin/bash
> for i in `cat unpw.csv`; do
>  UN=`echo $i | cut -f1 -d','`
>  PW=`echo $i | cut -f2 -d','`
>  ENCPW=`echo $PW | mkpasswd -s`
>  echo useradd -p $ENCPW -m $UN
> done


Thanks for posting this.  I didn't know about mkpasswd.  It seems to do 
what my perl script did, only better.  I guess I don't need that perl 
script anymore.  You didn't use a salt, though, and I wonder if that's OK. 
I also wonder how people usually generate salts.

Here I just made up a slant and compare my perl script with mkpasswd:

$ ( password_encrypt.pl pass_test '$6$RFQYCw9b$' ; mkpasswd --salt=RFQYCw9b --method=sha-512 pass_test ) | uniq
$6$RFQYCw9b$9wgEGg9ODG9aBFEp1vHsuufRfH2nPJ/ml88xPxMqr1C8qdW2uncIWOOj7zEoJWHHaHF8SdOvGtFs/kAYX0Glj1

They give identical output.  Note that the salt for my script includes the 
method as $6$ and it ends with another dollar sign, but mkpasswd leaves 
off the method and final dollar sign in the salt input, but it adds them 
for the output string.  That method makes sense to me, but they both work.


A tip on Bash scripting:

The Bash development team has deprecated backtick command substitution in 
favor of the $(...) sytax.  So your script could be written like this:

for i in $(at unpw.csv); do
   UN=$(echo $i | cut -f1 -d',')
   PW=$(echo $i | cut -f2 -d',')
   ENCPW=$(echo $PW | mkpasswd -s)
   echo useradd -p $ENCPW -m $UN
done

I found it easy to learn to like that syntax because we can nest the 
$(...) command substitutions.  More:

http://mywiki.wooledge.org/BashFAQ/082

But it looks like you can get away with this:

for i in $(at unpw.csv); do
   UN=$(echo $i | cut -f1 -d',')
   ENCPW=$(echo $i | cut -f2 -d',' | mkpasswd -s)
   echo useradd -p $ENCPW -m $UN
done


Best,
Mike