i am having a file, which contains usernames and encrypted passwords (openssl passwd) in the format user:password.
Now i want to change the password of this User with a Cronjob once a week.
With the help of Janos i made a Script, which changes the password to a $RANDOM generated value, and saves the encrypted password in pw.txt, and the non-encrypted in randompw.txt

cut -d: -f1 pw.txt | while read -r user; do
    echo "$user:$(openssl passwd $r)"
done > newpw.txt
mv newpw.txt pw.txt
echo $r > randompw.txt

So my problems are:
1.) With this, i just have a random generated value for each users, but i want a random value for each user (each line in the file).
2.) It would be good, if i can get the username and the cleartext password of each user into randompw.txt currently, i just have one $RANDOM Password there.

Does anyone has an Idea?

Old post


You can save the generated password in a variable, and write it two files:

  • One file in clear
  • One file hashed

For example:

# initialize (truncate) output files
> clear.txt
> hashed.txt

cut -d: -f1 pw.txt | while read -r user; do        
    # generate a hash from a random number
    hash=$(openssl passwd $RANDOM)

    # use the first 8 letters of the hash as the password

    # write password in clear formats to one file, and hashed in another
    echo "$user:$pass" >> clear.txt
    echo "$user:$(openssl passwd $pass)" >> hashed.txt
  • Thank you, perfect! No im just trying to generate Emails out of it, i hope i get it. :) – Manu_H Jan 6 '17 at 13:52
  • Next question about this topic: unix.stackexchange.com/questions/335390/… – Manu_H Jan 6 '17 at 15:40
  • @Manu_H that question is likely to get closed, as this is not a script writing service. If you try to implement it and get stuck, post the partially working code, and then somebody will probably help you finish it. – janos Jan 6 '17 at 15:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.