使用openssl

时间:2015-10-14 21:05:30

标签: linux bash openssl passwd

我尝试使用openssl生成一些MD5哈希值,以便与chpasswd一起使用

实施例。 CSV文件:

Sample,User,SU,,sauser,password
Test,User,TU,,teuser,password
User, T Test,TEST,,username,password

我创建的脚本:

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

但是,如果我从该脚本生成任何MD5并尝试将其与chpasswd一起使用,则无效。

echo 'username:$1$K8m2T$gb3C0Sz4JlXyewe8VRhxv.' | chpasswd -e

此密码将失败

如果我尝试在没有脚本的情况下手动执行此操作:

echo "username:"$(openssl passwd -1 -salt salt password) | chpasswd -e

1 个答案:

答案 0 :(得分:1)

您的CSV文件可能包含回车符,该回车符包含在密码字段中(它是最后一个字段)。

在处理之前,请务必在CSV上运行dos2unix或使用tr -d '\r'