I have a file which contains name of Users I want to create and assign them random password.I'm using this code but only first user from file is created and given random password.Here's the shell script.
#!/bin/bash
while IFS='' read -r line || [[ -n "$line" ]]; do
arr=($line)
sudo adduser --force-badname ${arr[0]} --quiet --disabled-password
var2=$(openssl rand -base64 2)
echo "${arr[0]}:$var2" | sudo chpasswd
echo "User ${arr[0]} Created "
echo "Password for ${arr[0]} is $var2"
done < "$1"
text in file I'm giving input is as
Tyr3
Try2
but only user Tyr3 is created. Here's the output:
Related
I am now trying to create users in Ubuntu from a text file and it looks like this:
student1
student2
student3
student4
student5
However, I keep getting invalid user name error. For instance
'seradd: invalid user name 'student5
Here is my code. The first argument is input file and the second input is output file. Can anyone help?
#!/bin/bash
if test ${#} -lt 1
then
echo "Please provide the input file"
exit 1
else
cat ${1} | while read user
do
randompw=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)
useradd -m -s /bin/bash ${user}
echo ${newuser}:${randompw} | chpasswd
if test $# -lt 2
then
echo ${newuser}:${randompw} >> pwlist.txt
else
echo ${newuser}:${randompw} >> ${2}
fi
if id -u ${user}
then
echo "User account ${user} created successfully"
else
echo "User account ${user} created unsuccessfully"
fi
done
fi
The variable newuser is not defined. I think you meant $user instead.
Suggestion:
Enclose variable references and computations within double quotes. I fixed that.
#!/bin/bash
if test ${#} -lt 1
then
echo "Please provide the input file"
exit 1
else
cat "${1}" | while read user
do
randompw="$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | head -n 1)"
useradd -m -s /bin/bash "${user}"
echo "${user}:${randompw}" | chpasswd
if test $# -lt 2
then
echo "${user}:${randompw}" >> pwlist.txt
else
echo "${user}:${randompw}" >> ${2}
fi
if id -u "${user}"
then
echo "User account ${user} created successfully"
else
echo "User account ${user} created unsuccessfully"
fi
done
fi
I've got basic knowledge of bash scripting, and need to create a function to create a user with custom settings:
1.User name and password
2.Group and user ID
3.Comment
4.Home directory
`function user_custom {
if [ $(id -u) -eq 0 ] ; then
read -p "Enter name here: " username
read -s -p "Enter password: " password
grep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0]; then
echo -e "${LIGHTGREEN}This user already exists! Try another name
please.${RESET}" ; break
else
pass=$(perl -e 'print crypt(($AVG[0], "password")' $password)
read -p "Enter group for user: "GID
egrep "^$GID" /etc/passwd > /dev/null
if [ $? -eq 0] ; then
$GID=$?
else
echo "No such group"
fi
read -p "Enter custom comment for $username: "comment
read -p "Enter unique user ID" Uid
getent passwd `grep "^Uid" /proc/$3/status |awk '{printf "%4s",$2}'` | if
[$? -eq $Uid ] ; then
echo "Select diferent ID"
fi
useradd -m -p -u -c -g $username $pass $Uid $comment $GID
fi
}`
I have a function ready, but I'm sure there are a lot of obvious mistakes.
Its like a patched skirt, I've gathered a lot of data from different sources.
I would be thankful if someone could help with fixing it.
Ill provide a screenshot of the whole function.
I'm from Ukraine, but i am good at english\russian.
PS: btw the 'if' in the end is included, all ifs are closeTd
Thanks ahead! <3
I correct it for you, I hope this will suit your need. You had lot of typo like if [ anything ]; then there is always needed space after [ and before ].
Brake only working for loops, you need to use exit 1 to terminate function with error code 1.
Instead of encrypt password in the script I prefer let passwd do the job, it will also enforce password rules.
The following way you can pass almost everything to command, like you typed it in from console:
(echo $password ; echo $password ) | passwd $username
I would add a lot of other check to make this script to enforce followings:
Uid should be number, and less/equal than UID_MAX in /etc/login.def
Gid should be a number, and less than the GID_MAY in /etc/login.def
And some feature as well like:
The script cannot handle if you like to make a GID for the user insted of using a existing one.
Asking if it should create home folder or not (currently -m do it in useradd)
If passwd fail because of password rule enforcing on the host, there is no retrying so you need to type it manually.
This is just the ones came into my mind now but there could be more.
#!/bin/bash
function user_custom {
if [ $(id -u) -eq 0 ] ; then
read -p "Enter name here: " username ;
read -s -p "Enter password: " password ; echo
grep "^$username" /etc/passwd > /dev/null
if [ $? -eq 0 ] ; then
echo -e "\nThis user already exists! Try another name please.\n" ; exit 1
else
read -p "Enter group for user: " GID
egrep "^"$GID":" /etc/group > /dev/null
if [ $? -gt 0 ] ; then
echo "No such group ( "$GID" )"; exit 1
fi
read -p "Enter custom comment for $username: " comment
read -p "Enter unique user ID: " Uid ; echo
if [ $(getent passwd | awk -F ":" '{print $3}' | egrep -c "^"$Uid"$" ) -gt 0 ];then
echo " ID already exist! " ; exit 1
fi
fi
useradd -m -u $Uid -c $comment -g $GID $username &&
(echo $password ; echo $password ) | passwd $username
fi
}
user_custom
I have a bash script called AddUsers.sh. The script should take a line like this:
john.mccarthy#caltech.edu;1927/09/04;sudo,visitor;/visitorData
and return variables like this:
name=john
surname=mccarthy
bdate=1927/09/04
uname=mccjoh
pass=1927
groups(array)=sudo, visitor
folder=/visitorData
When I run the script and give it the required text file, it is tellilng me that 'groupadd' 'chgrp' 'chmod' and 'chage' are all errors. Is anyone able to tell me why / give me any feedback?
Thanks in advance for any help.
#!/bin/bash
#check for file
while [ ! -f $file ]
do
#ask user for filename
echo What is the filename?
#reading input as file name
read file
if [ ! -f $file ]
then
echo "File not found!"
else
echo "File found!"
fi
done
#process each line and make a user from the data
cat "$file" | while read line
do
name=`echo $line | sed 's/^\(.*\)\..*\#.*$/\1/g'`
surname=`echo $line | sed 's/^.*\.\(.*\)\#.*$/\1/g'`
bdate=`echo $line | sed 's/^.*;\(.*\);.*;.*$/\1/g'`
#set groups to tokenize
groups=`echo $line`
folder=`echo $line`
temp2=`echo $name | sed 's/^\(...\).*$/\1/g'`
temp1=`echo $surname | sed 's/^\(...\).*$/\1/g'`
user="${temp1}${temp2}"
#pass must be first 4 numbers of birthdate
pass= ${bdate:0:4}
#tokenise group + add to array
declare -a groupArray
IFS=" "
groupArray=(`echo $groups | tr "," " "`)
#create groups if not existing.
for i in ${groupArray[#]}
do
if [ getent group $i ]
then
echo "group exists"
else
groupadd $i
fi
done
#Create shared folders if not existing.
if [ ! -d $folder ];
then
mkdir -p $folder
fi
#Create groups for shared folders
gname=`echo "${folder:1}"`
groupadd $gname
#Set group as owner of directory and change permissions
chgrp -R $gname $folder
chmod -R 770 $folder
#create user and add to groups
if [ grep "^${user}:" /etc/passwd ]
then
echo "user exists already!"
else
#Create user
useradd -m -d /home/$user -p $pass $user
#Add user to groups
for i in ${groupArray[#]}
do
usermod -a -G $i $user
done
#Add user to shared group
usermod -a -G $gname $user
fi
#force password change
chage -d 0 $user
done
you could use commands in scripts with full path address. and you can extract their full path address by "which" command.
for example instead of using "chmod" you could use "/bin/chmod"
type this to find full path:
$which chmod
I want to pass my n number csv data into command "esusers useradd username -r role -p password". How can i perform this in linux machine. i Have done this window but unable to do in linux machine can anyone help me out of this. My input will contain header also.
csv file user.csv:
user1,role1,pass1
user2,role2,pass2
user3,role3,pass3
The bash script (scripts.sh) to iterate over the csv file:
#!/bin/bash
# Check parameters
if [ "$#" -ne 1 ]; then
>&2 echo "Illegal number of parameters"
exit 1
fi
# Check file
if [ ! -f "${1}" ]; then
>&2 echo "File ${1} not found"
exit 1
fi
FILE="${1}"
while read line; do
USER=`echo ${line} | cut -d"," -f1`
ROLE=`echo ${line} | cut -d"," -f2`
PASS=`echo ${line} | cut -d"," -f3`
echo "adding user ${USER} (role: ${ROLE}) with password: ${PASS}"
esusers useradd "${USER}" -r "${ROLE}" -p "${PASS}"
done < ${FILE}
Then, add execution mode to the script with chmod +x script.sh
and run the script with the csv file as parameter ./script.sh user.csv
$ ./script.sh user.csv
adding user user1 (role: role1) with password: pass1
adding user user2 (role: role2) with password: pass2
adding user user3 (role: role3) with password: pass3
I am a newbie to linux scripting and I am getting an unexpected error. I have made a script which takes into account two options 1 and 2. I am having issues with option 2 I want to extract the usernames from a text file and add all of the users except the EOF to the home directory like this:
Try this for parsing your input file:
if [ -e $Path ]
then
while read user
do
[[ $user = \#* ]] && continue
Username=`echo $user | cut -f2 -d:`
if [ "$Username" != "EOF" ]
then
echo $Username
fi
done < $Path
fi