generate different password for each usr in list - bash

I use the following bash script to change password for users in list:
pass=`cat /dev/urandom | tr -dc 'a-zA-Z0-9-_!##$%^&*()_+{}|:<>?=' | fold -w 12 | grep -i '[!##$%^&*()_+{}|:<>?=]' | head -n 1`
for usr in `cat usrs.lst`
do
printf "Username is $usr and password is $pass\n"
done
The problem with the previous code it that doesn't change the password
meaning it gives one password for every user.
I need the previous script to give a different password for each user
so what am I missing in the previous code?

you should assign pass variable in loop as below;
#!/bin/bash
while IFS= read -r usr; do
pass=$(tr -dc 'a-zA-Z0-9-!##$%^&*()+{}|:<>?=' < /dev/urandom | fold -w 12 | grep -i '[!##$%^&*()_+{}|:<>?=]' | head -n 1)
printf 'Username is %s and password is %s\n' "$usr" "$pass"
done < usrs.lst

Related

How to create username from txt file

I am trying to create a user from a a text file called names.txt
(first and second field will be comment, third field will be username and forth field will be password)
Jessica Brown,jessicabrown,id0001
James Santos,jamessantos,id0002
This is what I have so far, but it is not working and I have a feeling I can do it a shorter way but can't figure it out.
user_name=$(cat names.txt | cut -d, -f3)
password1=$(cat names.txt | cut -d, -f4)
comment1=$(cat names.txt | cut -d, -f1 -f2)
user_name2=$(cat names.txt | cut -d, -f3)
password2=$(cat names.txt | cut-d, -f4)
comment2=$(cat names.txt | cut -d, -f1 -f2)
useradd "$user_name" -p "$password1" -c "$comment1"
useradd "$user_name2" -p "$password2" -c "$comment2"
To read the file and split the fields you may simply use read-while loop
Check man useradd for proper options for the command
Example (echo is here only for demonstration, remove it in final script):
#!/usr/bin/env bash
while IFS="," read -r COMMENT1 COMMENT2 USERNAME PASSWORD ; do
echo useradd "$USERNAME" --password "$PASSWORD" --comment "$COMMENT1,$COMMENT2"
done <names.txt

How to add shell script to jenkins pipeline

I have the below shell script:
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do
DIR=`echo $line | awk '{print$2}'`
Rep=`cat $DIR/repository-config |grep 'project\|repo' | tr '\n' ' '`
Size=`echo $line | awk '{print $1}' `
echo $Size $Rep
done
How can I run it thought Execute shell in Jenkins? I need also to add ssh command to the env (no need for a password).
Note I don't want to connect to the env and run this shell, but directly from Excecue shell box
If I'm not wrong your are using a Freestyle job and not a pipeline job.
Anyway, I think you have to try the following :
ssh -t XXXXX#YYYYY << 'EOF'
du -sh /bbhome/shared/data/repositories/* |sort -h |tail -20 |
while IFS= read -r line;do\
DIR=echo $line | awk '{print$2}'\
Rep=cat $DIR/repository-config |grep 'project\|repo' | tr '\n' ' '\
Size=echo $line | awk '{print $1}' \
echo $Size $Rep\
done
EOF
I've escaped the code inside your while loop using \, if it's doesn't works you can use ; instead.
If you want help for using a pipeline job, let me know but i might be a bit more complex.

Simple shell script that creates a dir with a random name

I'm trying to write a simple shell script in linux that creates directories with random names.
The names must be made from the date of the day followed by a random string
like in this example:
2018-02-22y2Fdv9zzLVLupkl9El0dWalJAGTROLxE
This is the shell script
#!/bin/bash
# the date
DATAOGGI= echo -n $(date +"%Y-%m-%d")
# random string
RANDOM_STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
# the dir
NEW_DIR=$(echo -n ${DATAOGGI}${RANDOM_STRING})
echo $NEW_DIR
mkdir $NEW_DIR
Unfortunately, even if the variable NEW_DIR is correct
echo $NEW_DIR -> 2018-02-22y2Fdv9zzLVLupkl9El0dWalJAGTROLxE
the name of the directory is
y2Fdv9zzLVLupkl9El0dWalJAGTROLxE
try just:
#!/bin/bash
DATAOGGI=$(date +"%Y-%m-%d")
RANDOM_STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
mkdir "${DATAOGGI}${RANDOM_STRING}"
apart from fact that it is not necessary in this example echo -n AFAIK has very inconsistent behavior and it is advised to use printf instead

bash scripting to add users

I created a bash script to read information such as username, group etc., from a text file and create users based on it in linux. The code seems to function properly and creates the users as desired. But the user information in the last line of the text file always gets misinterpreted. Even if i delete it then the next last line gets misinterpreted i.e., the text is read wrongly.
`
#!/bin/bash
userfile="users.txt"
IFS=$'\n'
if [ ! -f "$userfile" ]
then
echo "File does not exist. Specify a valid file and try again. "
exit
fi
groups=(`cut -f 4 "$userfile" | sed 's/ //'`)
fullnames=(`cut -f 1 "$userfile" | sed 's/,//' | sed 's/"//g'`)
username1=(`cut -f 1 "$userfile" |sed 's/,//' | sed 's/"//' | tr [A-Z] [a-z] | awk '{print substr($2,1,1) substr($3,1,1) substr($1,1,1)}'`)
username2=(`cut -f 4 "$userfile" | tr [A-Z] [a-z] | awk '{print substr($1,1,1)}'`)
i=0
n=${#username1[#]}
for (( q=0; q<n; q++ ))
do
usernames[$q]=${username1[$q]}"${username2[$q]}"
done
declare -a usernames
x=0
created=0
for user in ${usernames[*]}
do
adduser -c ${fullnames[$x]} -p 123456789 -f 15 -m -d /home/${groups[$x]}/$user -K LOGIN_RETRIES=3 -K PASS_MAX_DAYS=30 -K PASS_WARN_AGE=3 -N -s /bin/bash $user 2> /dev/null
usermod -g ${groups[$x]} $user
chage -d 0 $user
let created=$created+1
x=$x+1
echo -e "User $user created "
done
echo "$created Users created"
enter image description here`
#!/bin/bash
userfile="./users.txt"; # <-- Config
while read line; do
# FULL NAME
# Capture all between quotes as full name
fullname=$(printf '%s' "${line}" | sed 's/^"\(.*\)".*/\1/')
# Remove spaces and punctuations???:
fullname=$(printf '%s' "${fullname}" | tr -d '[:punct:][:blank:]')
# Right-side names:
partb=$(printf '%s' "${line}" | sed "s/^\".*\"//g")
# CODE 1, capture second row
code1=$(printf '%s' "${partb}" | cut -f 2 )
# CODE 2, capture third row
code2=$(printf '%s' "${partb}" | cut -f 3 )
# GROUP, capture fourth row
group=$(printf '%s' "${partb}" | cut -f 4 )
# Print only for report
echo "fullname: ${fullname}\n code 1: ${code1}\n code 2: ${code2}\n group: ${group}\n"
done <${userfile}
Maybe these are the fields that you want, now you have it in variables for manipulate them: $fullname, $code1, $code2 and $group.
Although maybe the fail that you observed was due to some misplaced quotation mark in the text file or the line breaks, on the attached screenshot I can see one missed quote.

Bulk account creation from text file

I have the following script, it reads from users.txt the first and second fields and uses them to generate the username and password and creates the accounts for each line. the problem is that the script is only creating accounts for the first 2 lines and not for the rest
#!/bin/bash
FILE=/home/knoppix/users.txt
USERSH=/bin/bash
while IFS=":" read GECOS USRGRP ; do
groupadd -f $USRGRP
USERNM="u$(cat /dev/urandom| tr -dc '0-9' | fold -w 6| head -n 1)"
USERPW=$(cat /dev/urandom| tr -dc 'a-zA-Z0-9' | fold -w 6| head -n 1)
useradd $USERNM -p $USERPW -g $USRGRP -c "$GECOS,$USRGRP" -d $HOME/$USERNM -s $USERSH -m
ACCNT=$(grep $USRNM /etc/passwd)
echo "${tgrn}Account creation successful!${tr}"
echo "$ACCNT"
echo "Credentials"
echo "${tred}Username:${tr} $USERNM ${tred}Password:${tr} $USERPW"
echo
done < $FILE
#!/bin/bash
while IFS=: read GECOS USRGRP; do
# your groupadd and useradd commands here
done < /home/knoppix/users.txt
#!/bin/bash
for line in $file; do
# make the account
done
rm $file
touch $file

Resources