bash create user with password: password not set as expected - bash

In I want to set a username and password non-interactively, but the password is not getting set correctly.
create_user.sh
user=username
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
echo $user $pass
useradd -m -p $pass $user
In the terminal:
$ sudo ./create_user.sh
username pa8fg5oAyLo8g
$ tail -1 /etc/passwd
username:x:1004:1004::/home/username:
$ su - username
Password: password
su: Authentication failure
What am I doing wrong?
UPDATE
This works, but it prints username password to the terminal window, which may not be desirable, and it requires hard-coded values:
create_user.sh
user=username
pass=password
useradd -m username
echo 'username:password' | chpasswd
In the terminal:
$ sudo ./create_user.sh
username password
$ su - username
Password: password
username#hostname:~$

My syntax was wrong. Here is a working version:
create_user.sh
user=username
pass=password
salt=Az # or any 2-character string from [A-za-z]
# Encrypt the password
pass=$(perl -e 'print crypt($ARGV[0], $salt)' $pass)
echo $user $pass
useradd -p $pass -m $user
In the terminal:
$ sudo ./create_user.sh
username AzSzB2uy8JFlk
$ su - username
Password: password
username#hostname:~$

Related

How to set the password for a new created in bash user using awk

Need some help in assigning a password to each newly created user from a text file using awk.
For example:
Text file:
John Doe 12345678
Jane Doe 87654321
Newly created user:
JDoe5678 with password: 12345678
JDoe4321 with password: 87654321
My current code:
#!/bin/bash
PATH_EMPLOYEE_FILE="employeelist"
password=($(awk '{print {print $3}))}' "${PATH_EMPLOYEE_FILE}"))
groupname="group1"
USERS_LIST=($(awk '{print substr($1,1,1) $2 substr($3,length($3)-3,length($3))}' "${PATH_EMPLOYEE_FILE}"))
for USER in "${USERS_LIST[#]}"
do
echo "User account created: ${USER}"
useradd -m -G "${groupname}" "${USER}" -p ${password}
done
You're not indexing $password, so you're always using the first password in the useradd command.
There's no need for awk or arrays, you can use bash's read command, and its parameter expansion operators to extract parts of the first name and password into the username.
while read -r fname lname password; do
username=${fname:0:1}$lname${password: -4} # don't forget the space before -4
echo "User account created: $username"
useradd -m -G "$groupname" "$username" -p "$password"
done < "$PATH_EMPLOYEE_FILE"

stdin not working in shell while creating password of newuser

This is what i am doing but getting error .
Ask for username
read -p "Enter your username to create: " USER_NAME
#Ask for real name
read -p "Enter your real name of account user: " REAL_NAME
#Ask for password
read -p "Enter your password for account:" PASSWORD
#Create User
useradd -c "${REAL_NAME}" -m ${USER_NAME}
#Set password for user
echo ${PASSWORD} | passwd --stdin ${USER_NAME}
#Prompt user to change password
passwd -e ${USER_NAME}
The error that i am getting is below
Error -
passwd: unrecognized option '--stdin' Usage: passwd [options] [LOGIN]

How do I use a for loop to execute a statement for two variables?

isql -U username -P password -S servername
I want to execute above statement repeatedly for five times, changing username and password each time in a for loop.
How can I do that?
When you don't care that your password can be seen by ps -ef, you can do
cat credentials.txt
user1 pw1
user2 pw2
user3 pw3
user4 pw4
user5 pw5
while IFS= read -r u p; do
echo "User $u"
isql -U "$u" -P "$p" -S servername
done < credentials.txt

nagios-cgi password screen bypass

I am trying to set up a bash script to install nagios3 and all of its dependencies. i understand that:
apt-get install -y nagios3
takes care of all of that.
what now what im worried about is bypassing the setup screens for nagios3-cgi
i have this so far:
#!/bin/bash
PASS="0"
REPASS="1"
while [ $PASS != $REPASS ]; do
read -s -p "Password: " PASS; echo
read -s -p "Retype: " REPASS; echo
done
debconf-set-selections <<< "postfix postfix/mailname string your.hostname.com"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
apt-get install -y postfix
apt-get install -y nagios3
i was able to bypass the postfix conf screen with this:
debconf-set-selections <<< "postfix postfix/mailname string your.hostname.com"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
how can i do the same thing with nagios3-cgi?
i tried this but it didnt work:
mkdir /etc/nagios3
htpasswd -cb /etc/nagios3/htpasswd.users nagiosadmin $PASS
I solved my own question.
first i did a normal install of nagios3 on a vm
then i used
debconf-get-selections > file.txt
debconf-get-selections >> file.txt
this writes all the installation details to file.txt
then search the file for the nagios3-cgi configuration
i found that the name of the config files i needed were
nagios3-cgi nagios3-cgi/adminpassword
and
nagios3-cgi nagios3-cgi/adminpassword-retype
then i did the same thing i did with the posfix install.
this was my final script. really simple.
PASS="0"
REPASS="1"
#Password loop
while [ $PASS != $REPASS ]; do
read -s -p "Nagios Password: " PASS; echo
read -s -p "Retype Nagios Password: " REPASS; echo
done
sudo debconf-set-selections <<< "postfix postfix/mailname string diggalabs.com"
sudo debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
sudo debconf-set-selections <<< "nagios3-cgi nagios3/adminpassword string $PASS"
sudo debconf-set-selections <<< "nagios3-cgi nagios3/adminpassword-repeat string $PASS"
sudo apt-get install -y nagios3

creating username and set password remotely

I am running script which generated strong password and takes input as user name and calls
remote script to create username and password .
script goes like this
USERNAME=$1
PASS=`cat /dev/urandom|tr -dc 'a-zA-Z0-9-!##%()+{}$|:?='|fold -w 10 | head -n 1| grep -i '[!##%()+{}|:$?=]'`
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh $USER $PASS "
It works fine ,if the password does not contain any extra characte like | , & and $ .
e.g.
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh testuser1 12345 "
it fails with strong password as follows .
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh testuser1 v|9q4TT8={ "
Is there any workaround for this .
Regards
Use enclosing " " to use strong password, bash will treat any character between " " as String.
ssh -i /home/ubuntu/test.pem ubuntu#192.168.10.32 "sudo /bin/bash /root/useradd.sh \"$USER\" \"$PASS\" "
And don't forget to escape inner quotes. i.e. add like \"

Resources