I'm trying to generate some MD5 hashes with openssl
for use with chpasswd
Ex. CSV file:
Sample,User,SU,,sauser,password
Test,User,TU,,teuser,password
User, T Test,TEST,,username,password
Script I created:
#!/bin/bash
file=$(readlink -f "$1") # open csv
while read line; do
salt=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 5 | head -n 1) #randomly generate 5 char salt
user=$(echo "$line" | cut -d, -f5 | xargs) # cut user from csv and trim with xargs
pass=$(echo "$line" | cut -d, -f6 | xargs) # cut pass from csv and trim with xargs
echo "$user:"$(openssl passwd -1 -salt "$salt" "$pass") >> ./global_chpasswd.data # gen MD5 hash per user and store in file
done < "$file" # close csv
However, if I take any MD5 generated from this script and try to use it with chpasswd it does not work.
echo 'username:$1$K8m2T$gb3C0Sz4JlXyewe8VRhxv.' | chpasswd -e
This password will fail
If I try to do this without the script by hand it works:
echo "username:"$(openssl passwd -1 -salt salt password) | chpasswd -e
Your CSV file probably has carriage returns which is being included as part of the password field (it's the final field).
Be sure to run dos2unix
or use tr -d '\r'
on your CSV before processing it.