Ne te casse pas le bulbe, fais
echo $username:$userpasswd|chpasswd
Je ne connais pas mkpasswd, dommage, ça m'aurait évité d'écrire ce qui suit
(découpé à vif d'un script existant, adaptations probablement nécessaires)
#=======================================================================
# random_chars
#=======================================================================
# Selects unique chars at random from a list
# Parameters
# Chars to choose from in $1
# Nb of chars to return in $2
# Returns
# Concatenates the result in $lst_out
#
# Warning : characters may be duplicated
function random_chars
{
local lst_in lst_in2 nb_requested i j
lst_in=$1
let nb_requested=$2
if [ $debug_passwd ]; then
echo "nb_requested=$nb_requested"
fi
declare -i i=0 j=0
while [ $i -lt $nb_requested ];
do
j=$(($RANDOM%${#lst_in})) # get a letter at random
lst_out=$lst_out${lst_in:j:1} # add it to the outstring
lst_in2=""
# Remove from the list of the possible chars
if [ $j -gt 0 ]; then
lst_in2=${lst_in:0:j}
fi
if [ $j -lt $((${#lst_in}-1)) ]; then
lst_in2=$lst_in2${lst_in:j+1}
fi
lst_in=$lst_in2
if [ $debug_passwd ]; then
echo "lst_in=$lst_in"
fi
let i=$i+1
done
if [ $debug_passwd ]; then
echo "random_chars returns $lst_out"
fi
}
#=======================================================================
# make_passwd : fabrique un mot de passe aleatoire
#=======================================================================
# Creates a passwd made of 2 special chars, 2 digits, 6 alpha
# No parameters
#
# Return
# Result returned in $password
function make_passwd
{
if [ $debug_passwd ]; then
echo "Inside make_passwd"
fi # debug
local lst_out lst_out2 specials numerics letters
specials='&#()+-_$*:;?<>='
numerics="0123456789"
letters="abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"
# Get the chars
lst_out=""
random_chars $specials 2
if [ $debug_passwd ]; then
echo $lst_out
fi
random_chars $numerics 2
if [ $debug_passwd ]; then
echo $lst_out
fi
random_chars $letters 6
if [ $debug_passwd ]; then
echo $lst_out
fi
# Scramble the whole password
lst_out2=$lst_out
lst_out=""
random_chars $lst_out2 10
passwd=$lst_out
echo "make_passwd retourne $passwd"
}
-- Jean-Marc