[SOLVED] Fetch username and password from file

Issue

So I’m automating ocserv set up through shellscript, and I have created random usernames and passwords which I have in a separate file. So the goal is to read those files into mapfile. This is what I have:

usern=$(wc -l < /home/ubuntu/randomusern.txt)
mapfile password < '/home/ubuntu/randompassword.txt'
mapfile username < '/home/ubuntu/randomusern.txt'
for((i=1; i<$usern; i++))
do
 allUser=$(echo ${username[@]})
 allpass=$(echo ${password[@]}|ocpasswd -c "/etc/ocserv/ocpasswd" ${allUser})
done

But when I check ocpasswd. there is only one user by the name "{username[@]}"
Any suggestion?
Thanks!!

Solution

There are several issues with your script: your loop is useless because it does the same in each iteration, you try to add all users at once, which is apparently not supported… You could try the following but I doubt it will work because the way ocpasswd gets the password is probably more complex than just reading it twice from the standard input (not tested, I don’t have ocpasswd):

local -i i n

mapfile password < /home/ubuntu/randompassword.txt
mapfile username < /home/ubuntu/randomusern.txt

if (( ${#password[@]} < ${#username[@]} )); then
  n=${#password[@]}
else
  n=${#username[@]}
fi

for (( i=0; i<n; i++ )); do
  u="${username[i]}"
  p=${password[i]}$'\n'${password[i]}
  ocpasswd -c "/etc/ocserv/ocpasswd" "$u" <<< "$p"
done

Answered By – Renaud Pacalet

Answer Checked By – Cary Denson (BugsFixing Admin)

Leave a Reply

Your email address will not be published.