0

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

Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
imhere
  • 3
  • 2
  • What is this `$'\n'{username[@]}`? What do you think it does? And what is this `| |` operator? Did you mean a single pipe or did you mean the logical OR (no space between the vertical bars)? And do you realize that your loop does the same thing in each iteration? – Renaud Pacalet Jun 22 '22 at 13:39
  • `||`, not `| |`. – chepner Jun 22 '22 at 13:43
  • @chepner I bet it should be a pipe ;-). – Renaud Pacalet Jun 22 '22 at 13:45
  • @RenaudPacalet Probably :) I have syntax tunnel vision :( – chepner Jun 22 '22 at 13:46
  • @RenaudPacalet I was trying to see the output first in a newline. And yes I was thinking of single pipe sorry typed a bit fast. I have edited now – imhere Jun 22 '22 at 13:50
  • First you should get rid of the useless loop. Why use a loop if all iterations do exactly the same?. Next you should check the content of your two arrays (`printf '%s\n' "${arrayname[@]}"`). And you don't need `allUser` at all. Use `"${username[@]}"`, instead. Moreover you don't need `allpass` (that you don't use after assigning it, and that may hide some interesting `ocpasswd` output. – Renaud Pacalet Jun 22 '22 at 13:58
  • Do you know what this `ocpasswd` command expects? As you use it it's apparently a list of arguments that are usernames, and the corresponding passwords on the standard input. Are you 100% sure it is how it works? Isn't it one single username and one single password, instead? – Renaud Pacalet Jun 22 '22 at 14:01
  • @RenaudPacalet Yeah it expects a username and passoword, in the form: `ocpasswd user1` and hit enter and put in password two times to make sure it matches – imhere Jun 22 '22 at 14:05

1 Answers1

1

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
Renaud Pacalet
  • 25,260
  • 3
  • 34
  • 51
  • Thanks a lot man! There is small thing in "${username[@]}", when I use this way it does not create empy user, but when I unquote that variable it creates the users that are in the file, any guess where that comes from. Other than that you saved my day, thanks a lot! – imhere Jun 22 '22 at 14:19
  • What I mean by empty user is the correct way a user is stored is like this: snack-swoosh:*:$5$iFMt8WBbuR.iO7Xk$87MLiAe28t69VRHa3quqQj.aywvx1SMFuY84BynRgw5 BUT when the qoute is used around that variable it becomes like this: :$5$iFMt8WBbuR.iO7Xk$87MLiAe28t69VRHa3quqQj.aywvx1SMFuY84BynRgw5 – imhere Jun 22 '22 at 14:20
  • If your usernames are normal (no spaces in them) with or without the double quotes around `${username[@]}` should not make any difference. So no, I have no idea. – Renaud Pacalet Jun 22 '22 at 14:25
  • Oops, sorry, I had forgotten to increment the loop index. The final `(( i += 1 ))` was missing. As a consequence, if it worked, all users got the same password, which was the first one. – Renaud Pacalet Jun 22 '22 at 14:28
  • Last update, slightly cleaner and more robust. – Renaud Pacalet Jun 22 '22 at 15:02