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

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"

Related

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

bash create user with password: password not set as expected

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:~$

Script to automate kadmin principal

I am trying to write a script to automate kerberos principal and but the script is not working. Not sure what i am missing. Below is the code:
#!/bin/bash
read -p "Please enter space-delimited list of principal to create: " NEW_Principal
clear
echo "password" | kinit adminuser/admin ; echo "password" | kadmin
for i in $NEW_ORGS
do
addprinc ${i}
${i}
echo ""
done
Also tried writing this way:
################### create principal #############
pass="principal_password"
echo "password" | kinit adminuser/admin
printf "%b" "password" | kadmin
printf "%b" "addprinc principal_name\n$pass\n$pass"
But its not working in script.
I have done it. All I have to do is pass the command as a string. Here is the answer:
echo -e "${i}\n${i}" | kadmin -w "$krb_pass" -q "addprinc

encrypting/decrypting password stored in config file

I have a simple Bash script automating tasks which require password-based authentication.
Currently I store the credentials in plain text:
$ cat ~/.myconfig
username=foo
password=bar
Obviously that's bad - so I wonder whether there's a simple way to encrypt/decrypt the password using my public/private key pair. Using Yet Another Password for the encryption wouldn't gain much, so I want it to happen pretty much automatically.
I've done some research (around here and elsewhere), but am way out of my depth on this one...
You can store password into md5 sum, add some salt before.
create:
\#!/bin/bash
salt=12345_
protocol=sha1sum
read -p "Enter login: " username
read -p -s "Password: " pass1
read -p -s "Repeat: pass2
if [ "pass1 != pass2" ]; then echo "Pass missmatch"; exit 1; else password=pass1; fi
echo -en "$username " >> ./mypasswd
echo -e "${salt}${password} | $protocol | awk '{print $1}'" >> ./mypqsswd
read:
\#!/bin/bash
salt=12345_ #(samesalt)
protocol=sha1sum
read -p "Enter username: " username
read -p -s "Enter password: " password
if [ `grep $username ./mypasswd | awk '{print $2}' != `echo -e "`echo ${salt}${password} | $protocol | awk '{print $2}'`" ]; then echo -e "wrong username or password"; exit 127; else echo -e "login successfull"; fi
There's your code.
To automate your task means providing the password; it won't make a difference is you encrypt/obfuscate the password, you'll need to provide the decrypting too.
The only way around this dilemma is an agent-like program, as for example ssh-agent, which stores your passwords for you.
(edit: corrected link)
If you simply want to hide the password then store its SHA1 hash. The compare the hash of the entered password with your stored hash.

Resources