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

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

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]

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

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 \"

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