Linux Shell script to add a user with a password from a list - linux

I'm trying to modify a script that read usernames/password from a file which is like this:
user1 pass1
user2 pass2
user3 pass3
I can't get the script to read the space between the users and pass.
What can I use to delimit this space?
This is my code:
for row in `cat $1`
do
if [ $(id -u) -eq 0 ]; then
username=${row%:*}
password=${row#*:}
#echo $username
#echo $password
I know I have to change the stuff in ${row%:} and ${row%:}
What do I have to put so it sees the space between user1 pass1 ?

It would be easier to split the two fields as you read each line. You can do that with read. It's also better to use a while loop here (a for loop requires to play with $IFS and it would also load the entire file in memory):
#!/bin/bash
if [ "$EUID" -ne 0 ]; then
echo >&2 "You are not root"
exit 1
fi
while read -r username password; do
# do the useradd stuff here
done < "$1"
Notice that I also changed $(id -u) to $UID which should be faster since it does not invoke an external program.

Related

While loop to test if a file exists in bash script

I am trying to create a loop to prompt the user to create a file. If the file already exists, it will keep prompting the user until the user enters a file that does not exist. But can't seem to get it to work, anyone can help, please?
file3=/root/simulate/$filename
while true ; do
if [ ! -f "$file3" ]; then
read -p "Please enter a file name to create in simulate folder:" filename
touch /root/simulate/$filename
elif [ -f "$file3" ]; then
read -p "Please enter a different file name as $filename already exist:" filename
touch /root/simulate/$filename
break
else
touch /root/simulate/$filename
fi
done
filename variable is empty.
There are two way to fill in filename variable
filename variable can pass on shell script by export command.
ex) $export filename=newFile
if you execute shell script, shell is forking and execute new shell. new shell taken your mother shell's Environment variables
Push filename variable into shell script.
note: I picked second way and I changed path of $file3.
I fixed codes in block of elif to ask again if something goes wrong.
this is a code.
#!/bin/bash
filename=newFile
file3="./$filename"
while true ; do
if [ ! -f "$file3" ]; then
read -p "Please enter a file name to create in simulate folder:" filename
touch ./$filename
break
elif [ -f "$file3" ]; then
while true ; do
read -p "Please enter a different file name as $filename already exist:" filename
if [ ! -f "$filename" ]; then
touch ./$filename
break 2
fi
done
else
touch ./$filename
fi
done

How to fix "id -u" returning 0 into my variable for if statement?

I'm trying to put the result of "id -u $1" into a variable in order to verify it with an if statement but it seems like it's returning 0 into my variable even tho I've checked it and it's supposed to be 1008.Is it because the username is taken from the argument of the script?
UID=`id -u $1`
LOCK=`usermod -L $1`
if test $UID -lt 500;then
echo "impossible to lock user"
else
$LOCK;
fi
Your problem is that you use system variable in your script and try to change it.
instead of UID try to use UID1 (for example) as variable name. And your script will be something like:
UID1=`id -u $1`
LOCK=`usermod -L $1`
if test $UID1 -lt 500;then
echo "impossible to lock user"
else
$LOCK;
fi

Change the primary and secondary group of a user using a script

So what I am trying to do is run a script, that can only be run from Root, and it creates new usernames and passwords. The script checks to see if that username and password already exist, and if it does, it will not create the new username. All of that works fine, but now, if a new user is created, I want to be able to add this new user to a group.
For example, lets say I want the primary group to be alpha and the secondary group to be beta. How would I be able to:
Set a user to just the primary group
Set a user to primary and secondary group
Set a user to only the secondary group
The following is my script:
#!/bin/sh
# Creating a script that creates a new user and password, runs in Root, accessible # in any shell, ADD USER TO SPECIFIC GROUPS NOW (itar and bfe already created)
ROOT_UID=0 #Root has $UID 0
SUCCESS=0
E_USEREXISTS=70
E_NOTROOT=65 #Not root
#Run as root, and this checks to see if the creater is in root. If not, will not run
if [ "$UID" -ne "$ROOT_UID" ]; then
echo "Sorry must be in root to run this script"
exit $E_NOTROOT
fi
if [ $# -eq 2 ]; then
username=$1
passwd=$2
grep -q "$username" /etc/passwd
#Checking if the username and password already exists
if [ $? -eq $SUCCESS ]; then
echo "User $username already exists"
echo "Please choose another username"
exit $E_USEREXISTS
fi
#Creating the new username and the new password:
useradd $username -d /home/$username -m ;
echo $passwd | passwd $username --stdin;
echo "The new user account is setup"
#Adding the user to group bfe or itar
echo "Which group will this user be in?"
read x more
#If the person does not give the correct number of arguments
else
echo "This program needs 2 arguments and you have given $#"
echo "You have to call the script and provide a username and password"
#if [ \"$x\" -eq "bfe" ]; then #echo "First word was \"$x\""
# usermod -g bfe $username
#if [ \"$x\" -eq "itar" ]; then
# usermod -g itar $username
fi
exit 0
Any help would be appreciated! Still learning the ropes so please take it easy on me.
Use -G option for useradd.
-G, --groups GROUP1[,GROUP2,...[,GROUPN]]]
A list of supplementary groups which the user is also a member of.Each
group is separated from the next by a comma, with no intervening
whitespace.
The groups are subject to the same restrictions
as the group given with the -g option.
The default is for the user to belong only
to the initial group.
Then rearrange part of your script, something like this
#Adding the user to group bfe or itar
echo "Which group will this user be in?"
read x
# Check if group exists
grep -q "$x" /etc/group || echo "Group doesn't exist"
#Creating the new username and the new password:
useradd "$username" -d "/home/$username" -m -G "$x" ;
echo "$passwd" | passwd "$username" --stdin;
echo "The new user account is setup"
You can use -G to assign more than one group to the user.

ssh to different nodes using shell scripting

I am using below code to ssh to different nodes and find if an user exists or not. If the user doesn't exist it will create it.
The script works fine if I don't do ssh but it fails if I do ssh.
How can I go through different nodes using this script?
for node in `nodes.txt`
usr=root
ssh $usr#$node
do
if [ $(id -u) -eq 0 ]; then
read -p "Enter username : " username
read -s -p "Enter password : " password
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then
echo "$username exists!"
exit 1
else
pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
useradd -m -p $pass $username
[ $? -eq 0 ] && echo "User has been added to system!" || echo "F
ailed to add a user!"
fi
else
echo "Only root may add a user to the system"
exit 2
fi
done
Your script has grave syntax errors. I guess the for loop at the beginning is what you attempted to add but you totally broke the script in the process.
The syntax for looping over lines in a file is
while read -r line; do
.... # loop over "$line"
done <nodes.txt
(or marginally for line in $(cat nodes.txt); do ... but this has multiple issues; see http://mywiki.wooledge.org/DontReadLinesWithFor for details).
If the intent is to actually run the remainder of the script in the ssh you need to pass it to the ssh command. Something like this:
while read -r node; do
read -p "Enter user name: " username
read -p -s "Enter password: "
ssh root#"$node" "
# Note addition of -q option and trailing :
egrep -q '^$username:' /etc/passwd ||
useradd -m -p \"\$(perl -e 'print crypt(\$ARGV[0], \"password\")' \"$password\")" '$username'" </dev/null
done <nodes.txt
Granted, the command you pass to ssh can be arbitrarily complex, but you will want to avoid doing interactive I/O inside a root-privileged remote script, and generally make sure the remote command is as quiet and robust as possible.
The anti-pattern command; if [ $? -eq 0 ]; then ... is clumsy but very common. The purpose of if is to run a command and examine its result code, so this is better and more idiomatically written if command; then ... (which can be even more succinctly written command && ... or ! command || ... if you only need the then or the else part, respectively, of the full long-hand if/then/else structure).
Maybe you should only do the remote tasks via ssh. All the rest runs local.
ssh $user#$node egrep "^$username" /etc/passwd >/dev/null
and
ssh $user#$node useradd -m -p $pass $username
It might also be better to ask for username and password outside of the loop if you want to create the same user on all nodes.

Unable to get bash if condiftion to work with multiple commands per outcome

I have been trying to get various versions of this to work:
[[ -e "~/usersl" ]] && { user=`cat ~/usersl`; echo Username: $user; } || { read -p "Username:" user; echo $user > ~/usersl; }
The main goal is to have a if..then..else all in one line. From what I have read on this site and others, this should work. It should check to see if the usersl file exists and if it does then it prints out the detected username and populates the $user variable. If the usersl file doesn't exist then it should ask for the username and populate the variable at the same time then record the new username into a file. Once that has been done the next tiem the command is ran the file already exists and it just displays the username and propagates the variable. Hope my explanation isn't overly complicated but the task is very simple.
Any help is much appreciated
Your most immediate bug had nothing to do with the conditional syntax, but was caused by a bug in how you were quoting around your test:
if [[ -e ~/usersl ]]; then
user=$(<~/usersl)
echo "Username: $user"
else
read -p "Username:" user
echo "$user" > ~/usersl
fi
As a one-liner, this would be:
if [[ -e ~/usersl ]]; then user=$(<~/usersl); echo "Username: $user"; else read -p "Username:" user; echo "$user" >~/usersl; fi
Tilde expansion is suppressed by quotes. If in doubt, write $HOME instead of ~.
Using a && b || c is bug-prone: If a succeeds and b tries to run but fails, you can have c run even though b was also invoked.

Resources