0

I'm in the process of making a bash script to automate adding a user. My password always have pattern, so I am setting that to a variable then... (this is where I am stuck -encrypting-). I found this post but that talks about -stdin which I don't want to use. Besides, I am asked for the password if I omit it anyway. So, how can I encrypt the password from my username automatically?

My script

#!/bin/bash
if [ $# -ne 1 ]
then
  echo "Usage: $0 <user>"
else
  p=${1%.*}
  p=$(echo $p | tr -d 'aeiou' | openssl passwd) $1
  useradd -p $p $1
fi

EDIT: mkpasswd on CentOS doesn't seem to work like the one mentioned in the post I linked.

EDIT2: bash version is 4.1.2(1)-release, if that matters.

2 Answers 2

2

You can use useradd and chpasswd to automate user creation. e.g.

useradd -c "Bob Smith" -m bob
echo "bob:mypassword" | chpasswd

If you don't want the plain text password to appear in the script, use the --encrypted flag:

echo "bob:encrypted_mypassword" | chpasswd --encrypted
1
  • I have a pattern when creating the password, so I'd rather stick to that. With that said, maybe it's time to lose the pattern and use standard stuff. Commented May 14, 2015 at 8:34
0

I ended up using this, inspired from this post

#!/bin/bash
if [ $# -ne 1 ]
then
  echo "Usage: $0 <user>"
else
  u=${2%.*}
  u=${u//[aeiou]}
  echo "$2:$u" > .tmp
  useradd $2
  < .tmp chpasswd
fi

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.